2012-02-21 30 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(); 
} 
相關問題