2012-02-21 249 views
0

如何设置DataGridViewComboBoxCell中不同项目的字体颜色?例如,如果我有10件物品,我会如何制作物品3和5红色,并让其他物品变黑?设置DataGridViewComboBoxCell中项目的字体颜色

编辑:这是一个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??? 
        } 

       } 
} 
+0

我下面贴一个快速和肮脏的方法..如果你不想做整个列/行然后更改对象即组合框细胞类似这应该有助于让你开始.. – MethodMan 2012-02-21 22:18:43

+0

可能的重复[要更改DataGridViewComboBoxCell颜色(样式)动态](http://stackoverflow.com/questions/7242308/to-change-the-datagridviewcomboboxcell-colorstyle-dynamically) – 2012-02-22 16:42:39

回答

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; 
       } 
      } 
     } 
    } 
+0

对不起,我应该注意到这些细节。这是为Winform应用程序和DataGridViewComboBox不是数据绑定。 – JonF 2012-02-21 22:26:19

+0

您是否仍然可以使用代码并将其放置在不同的事件处理程序中? – MethodMan 2012-02-22 15:00:41

+0

再次查看代码后,我意识到它会将整个组合框更改为新的颜色,而不是datagridviewcombobox中的单个项目,因此我将无法使用该代码 – JonF 2012-02-22 16:19:37

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(); 
}