【问题标题】:Color item in a datagridview combobox columndatagridview 组合框列中的颜色项
【发布时间】:2011-10-07 12:42:53
【问题描述】:

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

【问题讨论】:

    标签: c# .net winforms datagridview


    【解决方案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);
    

    【讨论】:

    • 如何在数据网格视图组合框列中捕捉组合框的事件?
    • DataGridViewComboBoxEditingControl 类的文档提供了一个示例,说明如何完全按照自己的意愿进行操作:msdn.microsoft.com/en-us/library/… 它显示了如何连接到网格中托管的组合框上的 SelectedIndexChanged 事件。
    【解决方案2】:

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

    看看this线程。

    【讨论】:

      猜你喜欢
      • 2015-12-08
      • 2017-05-27
      • 1970-01-01
      • 2011-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多