根据列值更新行字体颜色的DataGridView

5
我想问一下如何在dataGridView中根据列的值自动更新行的字体颜色。
例如,一个表有4个列:id,name,rentPayMent和check
检查每一行,看看是否有任何check == 0的值 如果是,则此行的字体color = red,否则do nothing 目前,我使用以下代码,但它会出现错误:

Object reference not set to an instance of an object,System.NullReferenceException was unhandled

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
我认为您应该参考这个链接,它可能会帮到您。staskoverflow - Jignesh.Raj
5个回答

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

最好给dataGridView中的列命名。例如,将列名为check命名为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;
                    }
                }
            }
        }


}

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
                } 

            }

        }

最好的祝福


我收到了错误信息,说找不到名为check的列。 参数名称:columnName,即使我尝试将列名更改为id或name,它仍然给出相同的警告。 - Kam2012
请查看我的更新帖子。您必须设置现有的数据网格列。确保您的列数据属性名称和标题文本相等。 - BizApps
仍然存在同样的问题,即使我使用了检查而不是“YourDatagridColumnName”,并且我已经设置了现有的datagrid列。列的数据属性名称和HeaderText也相等。 - Kam2012
你能否也发布一下你作为datagrid数据源使用的选择查询吗? - BizApps

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

-1
我个人会使用 JavaScript 处理这个逻辑。但如果非要在 CodeBehind 中实现,我会检查单元格是否有一个值为“0”。如果有,我会添加一个名为“red”的 CssClass(或任何你选择的名称),然后编写 CSS 以设置颜色值。这样做至少会使维护变得更加容易,而不是将颜色处理放在服务器端。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接