2012-12-11 94 views
0

我們有一個自定義組合框,用於顯示一些形狀而不是文本的窗體。要做到這一點,我只需重寫OnDrawItem函數並顯示我們想要的內容。下面是引用片段:DataGridView的自定義組合框列

protected override void OnDrawItem(DrawItemEventArgs e) 
{ 
    base.OnDrawItem(e); 

    e.DrawBackground(); 
    if (e.Index >= 0) 
    { 
     Brush brush = new SolidBrush(Color.LightGray); 

     int size = this.Height/2; 
     int origenX = e.Bounds.X + 1; 
     int origenY = e.Bounds.Y + 3; 
     System.Drawing.Drawing2D.GraphicsPath path = 
       new System.Drawing.Drawing2D.GraphicsPath(); 
     switch (e.Index) 
     { 
      case 0:        
       e.Graphics.FillRectangle(brush, origenX, origenY, size, size);        
       Rectangle r = new Rectangle(origenX, origenY, size, size);        
       ControlPaint.DrawBorder(e.Graphics, r, Color.Black, 
             ButtonBorderStyle.Solid); 
       break; 
      case 1: 
       path.AddEllipse(origenX, origenY, size, size); 
       e.Graphics.FillPath(brush, path); 
       e.Graphics.DrawPath(Pens.Black, path); 
       break; 
     } 
    } 
} 

因此,如果您添加到窗體,並添加到您的收藏幾個項目的所有你看到的是一個正方形,並在下拉了一圈。

好的,所以,我現在想要做的是將這個相同的組合框添加到DataGridView。我知道這個控件有一個DataGridViewComboBoxColumn。我試圖擴展控件,但是,我沒有看到這個OnDrawItem函數覆蓋。我猜那裏有類似的東西? 任何幫助,將不勝感激。 謝謝!

回答

0

您需要將DataGridViewComboBox作爲您的自定義Combobox進行混搭。

private void dgTest_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
     { 
      if (dgTest.CurrentCell.ColumnIndex == 0) // Which column ever is your DataGridComboBoxColumn 
      { 
       // This line will enable you to use the DataDridViewCOmboBox like your 
       // Custom ComboBox. 
       CustomComboBox combo = e.Control as CUstomComboBox; 

      } 
     } 
+0

感謝您的幫助!不幸的是,沒有這不做。我確實先嚐試過。這個解決方案有兩個問題,一個是它只顯示基本上是ID的項目中的數據。其次,一旦退出編輯模式,它不會顯示任何內容,因爲表格本身不知道要繪製什麼。 –