datagridview组合框列中的颜色项

时间:2011-10-07 12:42:53

标签: c# .net winforms datagridview

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

2 个答案:

答案 0 :(得分:1)

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

看看this主题。

答案 1 :(得分:1)

使用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);