datagridview组合框列中的颜色项目

问题描述:

我有一个带有组合框列的Winform datagridview。是否可以对组合框中的特定项目着色?如果是的话,我该怎么做(用C#)?datagridview组合框列中的颜色项目

使用ComboBox1_DrawItem

protected void ComboBox1_DrawItem(object sender, 
    System.Windows.Forms.DrawItemEventArgs e) 
{ 

    float size = 0; 
    System.Drawing.Font myFont; 
    FontFamily font= null; 

    //Color and font based on index// 
    Brush brush; 
    switch(e.Index) 
    { 
     case 0: 
      size = 10; 
      brush = Brushes.Red; 
      family = font.GenericSansSerif; 
      break; 
     case 1: 
      size = 20; 
      brush = Brushes.Green; 
      font = font.GenericMonospace; 
      break; 
    } 

    myFont = new Font(font, size, FontStyle.Bold); 
    string text = ((ComboBox)sender).Items[e.Index].ToString(); 
    e.Graphics.DrawString(text, myFont, brush, e.Bounds.X, e.Bounds.Y); 
+0

我怎样才能赶上组合框在数据网格视图组合框列的事件? – Elmex

+0

DataGridViewComboBoxEditingControl类的文档具有 一个示例,说明如何完全按照您的需要进行操作:http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxeditingcontrol.aspx它显示了如何连接到网格中承载的 组合框上的SelectedIndexChanged事件。 –

处理EditingControlShowing事件以在单元格进入edit mode时执行编辑控件的自定义初始化。

看看this线程。