在DataGridViewComboBoxCell中设置项目的字体颜色

时间:2012-02-21 21:47:34

标签: c# winforms datagridview datagridviewcomboboxcell

如何设置DataGridViewComboBoxCell中不同项目的字体颜色?例如,如果我有10个项目,我如何将项目3和5设为红色并将其他项目留为黑色?

编辑:这是针对winform应用程序而DataGridViewComboBox不是数据绑定

edit2:也许我可以在editcontrolshowing中这样做?

   private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
   {
            if (dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].Name == "MyCombo")
            {

                DataGridViewComboBoxCell comboCell = (DataGridViewComboBoxCell)dataGridView1.CurrentCell;

                for (int i = 0; i < comboCell.Items.Count; ++i)
                {
                    string contract = comboCell.Items[i].ToString();
                    if (contract.ToUpper().Contains("NO"))
                    {
                        // can I set this item have a red font color???
                    }

                }
}

2 个答案:

答案 0 :(得分:1)

对于以下行的

- 挂钩 OnRowDataBound 事件然后做东西

ASPX(网格):

    <asp:.... OnRowDataBound="RowDataBound"..../>

代码背后:

    protected void RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex == -1)
        {
            return;
        }

        if(e.Row.Cells[YOUR_COLUMN_INDEX].Text=="NO"){
             e.Row.BackColor=Color.Red;   
        }
    }

FOR WinForms:

hook the **DataBindingComplete** event and do stuff in it:

     private void dataGridView1_DataBindingComplete(object sender, 
                       DataGridViewBindingCompleteEventArgs e)
    {
        if (e.ListChangedType != ListChangedType.ItemDeleted)
        {
            DataGridViewCellStyle red = dataGridView1.DefaultCellStyle.Clone();
            red.BackColor=Color.Red;

            foreach (DataGridViewRow r in dataGridView1.Rows)
            {
                if (r.Cells["FollowedUp"].Value.ToString()
                       .ToUpper().Contains("NO"))
                {
                    r.DefaultCellStyle = red;
                }
            }
        }
    }

答案 1 :(得分:0)

您需要手动绘制ComboBox项。请尝试以下操作(基于this MSDN post):

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    // Note this assumes there is only one ComboBox column in the grid!
    var comboBox = e.Control as ComboBox;
    if (comboBox == null)
        return;

    comboBox.DrawMode = DrawMode.OwnerDrawFixed;
    comboBox.DrawItem -= DrawGridComboBoxItem;
    comboBox.DrawItem += DrawGridComboBoxItem;
}

private void DrawGridComboBoxItem(object sender, DrawItemEventArgs e)
{
    e.DrawBackground();

    if (e.Index != -1)
    {
        // Determine which foreground color to use
        var itemValue = actionsColumn.Items[e.Index] as string;
        bool isNo = String.Equals("no", itemValue, StringComparison.CurrentCultureIgnoreCase);
        Color color = isNo ? Color.Red : e.ForeColor;

        using (var brush = new SolidBrush(color))
            e.Graphics.DrawString(itemValue, e.Font, brush, e.Bounds);
    }

    e.DrawFocusRectangle();
}