Color item in a datagridview combobox column
I have a Winform datagridview with a combobox column. Is it possible to color a specific item in the comboboxes? If yes, how can I do 开发者_如何学编程this (in C#)?
Use 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);
Handle the EditingControlShowing event to perform custom initialization of the editing control when a cell enters edit mode.
Take a look at this thread.
精彩评论