2013-01-03 66 views
5

我想问一个行如何自动更新其字体颜色基于dataGridView的列值。(datagridview)基于列值更新行的字体颜色

例如,一个表格有4列:id, name, rentPayMent and check

检查每一行,看看是否有任何check == 0 价值如果是,那么该行的字体color = red在移动否则do nothing

,我使用的代码如下,但它带出误差

不设置为一个对象的一个​​实例

对象引用,System.NullReferenceException了未处理

private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) 
{ 
    foreach (DataGridViewRow row in dataGridView1.Rows) 
    { 
     if (row.Cells[3].Value.ToString() == "0") //**Object reference not set to an instance of an object** 
     { 
      row.DefaultCellStyle.BackColor = Color.Red; //then change row color to red 
     } 
    } 
} 

感谢所有,我已经得到了解决方案。

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
    { 
     if (dataGridView1.Rows[e.RowIndex].Cells[3].Value != null && !string.IsNullOrWhiteSpace(dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString())) 
     { 
      if (dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString().Trim() == "0") 
       dataGridView1.Rows[e.RowIndex].DefaultCellStyle = new DataGridViewCellStyle { ForeColor = Color.Red }; 
     } 
     else 
     { 
      dataGridView1.Rows[e.RowIndex].Cells[3].Style = dataGridView1.DefaultCellStyle; 
     } 

    } 
+1

我想你应该Refar此链接我要帮助你 [staskoverflow] [1] [1]: http://stackoverflow.com/questions/12202751/how-can-i-make-a-datagridview-cells-font-a-particular-color –

+1

winforms或asp.net? –

+0

它是在winforms中。 – Kam2012

回答

1
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
{ 
    if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null && !string.IsNullOrWhiteSpace(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString())) 
    { 
     dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style = new DataGridViewCellStyle { ForeColor = Color.Orange, BackColor = Color.Blue }; 
    } 
    else 
    { 
     dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style = dataGridView1.DefaultCellStyle; 
    } 
} 
0

我这样做对的winform。

试试下面我的代码:

private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) 
     { 

      foreach (DataGridViewRow row in dataGridView1.Rows) 
      { 

       if (row.Cells["YourDatagridColumnName"].Value.ToString() == "0") //if check ==0 
       { 
        row.DefaultCellStyle.BackColor = Color.Red; //then change row color to red 
       } 

      } 

     } 

问候

+0

我收到错误消息,找不到列命名检查。 参数名称:columnName,即使我尝试将列名更改为ID或名称,它仍然给我相同的警告。 – Kam2012

+0

请参阅我更新的帖子。您必须设置现有的datagrid列。确保您的列DataPropertyName和HeaderText相等。 – BizApps

+0

仍然有同样的问题,找不到检查,即使我使用检查,而不是“YourDatagridColumnName”,并且我已经设置了现有的datagrid column.and列DataPropertyName和HeaderText也是平等的 – Kam2012

-1

我个人会使用JavaScript处理这个逻辑。但是如果绝对必须将它放在CodeBehind中,我会检查单元格的值是否为“0”。如果是这样,我会添加一个“红色”(或任何你选择的名字)的CssClass,然后写入CSS来获得颜色值。这至少可以让维护更容易,而不是让服务器端处理颜色。

0

我用类似这样的东西:

private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) 
{ 
    foreach (DataGridViewRow row in dataGridView1.Rows) 
    { 
     if(!string.IsNullOrEmpty(row.Cells[3].Value.ToString())) // make sure the value is present 
     (
      if (row.Cells[3].Value.ToString() == "0") 
      { 
       row.DefaultCellStyle.BackColor = Color.Red; //then change row color to red 
      } 
     ) 
    } 
} 
0

它较受欢迎的名字在DataGridView列。例如将列检查命名为colCheck。 使用CellFormatting方法来更改行字体颜色如下

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
{ 

    if (e.RowIndex > -1 && e.ColumnIndex == dataGridView1.Columns["colCheck"].Index) 
     { 
      if (e.Value != null) 
      { 

       if (dataGridView1.Rows[e.RowIndex].Cells["colCheck"].Value.ToString() == "1") 
       { 
        for (int i = 0; i < this.dataGridView1.Columns.Count; i++) 
        { 
         this.dataGridView1.Rows[e.RowIndex].Cells[i].Style.ForeColor = Color.Red; 
        } 

       } 
       else 
       { 
        for (int i = 0; i < this.dataGridView1.Columns.Count; i++) 
        { 
         this.dataGridView1.Rows[e.RowIndex].Cells[i].Style.ForeColor = Color.Black; 
        } 
       } 
      } 
     } 


}