【问题标题】:(datagridview) update a row's font color base on a column value(datagridview) 根据列值更新行字体颜色
【发布时间】:2013-01-03 04:15:34
【问题描述】:

我想问一行如何根据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;
        }

    }

【问题讨论】:

标签: c# winforms


【解决方案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;
    }
}

【讨论】:

    【解决方案2】:

    我是在 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
                    } 
    
                }
    
            }
    

    最好的问候

    【讨论】:

    • 我收到错误消息,不是找不到名为检查的列。参数名称:columnName,即使我尝试将列名更改为 id 或 name,它仍然给我同样的警告。
    • 请看我更新的帖子。您必须设置现有的数据网格列。确保您的 Column DataPropertyName 和 HeaderText 相等。
    • 仍然有同样的问题,找不到检查,即使我使用检查而不是“YourDatagridColumnName”,我已经设置了现有的datagrid column.and Column DataPropertyName和HeaderText也相等
    • 您能否发布您用作数据网格数据源的选择查询
    【解决方案3】:

    我使用类似的东西:

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

    【讨论】:

      【解决方案4】:

      最好在 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;
                          }
                      }
                  }
              }
      
      
      }
      

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          猜你喜欢
          • 2018-05-12
          • 2015-10-09
          • 1970-01-01
          • 2023-04-06
          • 2017-04-06
          • 2017-02-05
          • 1970-01-01
          • 2015-09-02
          • 2015-07-26
          相关资源
          最近更新 更多