2011-08-30 157 views
1

我有一个DataGridview有几列,其中一列是DataGridViewComboBoxColumn。若要动态更改DataGridViewComboBoxCell颜色(样式)

这种情况是,当用户从组合框中选择一些值(选择索引> 0)时,所选单元格的整行将显示为白色。如果用户选择组合框的空值(选定索引为0),则整行将以黄色显示,并且此组合框单元应显示为红色。

我能够实现显示除了组合框以外的黄色整行。

private void grdCurve_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
     { 
      //for datatype column 
      if (grdCurve.CurrentCell.ColumnIndex == DATATYPE_COLUMN_INDEX) 
      { 
       // Check box column 
       ComboBox comboBox = e.Control as ComboBox; 
       comboBox.SelectedIndexChanged -= new EventHandler(comboBox_SelectedIndexChanged); 
       comboBox.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged); 
      } 
     } 

void comboBox_SelectedIndexChanged(object sender, EventArgs e) 
     { 
     if (selectedIndex >= 0) 
      { 
      if (!ValidateMnemonic(mnRow, row)) 
       { 
        MarkInvalidRow(row); 
       } 
       else 
       { 
        MarkValidRow(row); 
       } 
      } 
     } 

private void MarkInvalidRow(DataGridViewRow row) 
     { 
      DataGridViewCellStyle style = new DataGridViewCellStyle(); 
      if (m_InvalidRowTable.Count > 0) 
      { 
       if (m_InvalidRowTable.ContainsKey(row.Index)) 
       { 
        int col = Convert.ToInt32(m_InvalidRowTable[row.Index]); 
        DataGridViewCell cell = row.Cells[col]; 
        MarkInvalidRowColor(row); 
        style.BackColor = Color.Red; 
        style.SelectionBackColor = Color.Red; 
        cell.Style = style; 
        if (grdCurve.CurrentCell is DataGridViewComboBoxCell) 
        {       
         grdCurve.CurrentCell.Style = style; 
        } 
       } 
       else 
       { 
        MarkInvalidRowColor(row); 
       } 
      } 
      else 
      { 
       MarkInvalidRowColor(row); 
      } 

      m_InvalidRowTable.Remove(row.Index); 
     } 

private void grdCurve_CellValueChanged(object sender, DataGridViewCellEventArgs e) 
     { 
      //do nothing 
     } 

private void grdCurve_CurrentCellDirtyStateChanged(object sender, EventArgs e) 
     { 
      if (grdCurve.CurrentCell is DataGridViewCheckBoxCell) 
       grdCurve.CommitEdit(DataGridViewDataErrorContexts.Commit); 

      if (grdCurve.CurrentCell is DataGridViewComboBoxCell && grdCurve.IsCurrentCellDirty) 
       grdCurve.CommitEdit(DataGridViewDataErrorContexts.Commit); 

      grdCurve.EndEdit(); 
     } 

当我将组合框中的项目更改为空时,我预计组合框将被标记为红色。 但是,它显示为白色,当我点击其他一些单元格时,红色将在组合框单元格中更新。

请帮忙。

感谢, 普拉萨德

回答