5

我想问一行如何根据dataGridView.

例如,一个表有 4 列,它们是:id, name, rentPayMent and check.

检查每一行是否有任何值check == 0 If yes, then this row's font color = red Elsedo 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;
        }

    }
4

5 回答 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;
    }
}
于 2013-01-03T04:21:49.980 回答
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
                } 

            }

        }

此致

于 2013-01-03T04:30:55.897 回答
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
          }
        )
    }
}
于 2013-01-03T23:05:25.527 回答
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;
                    }
                }
            }
        }


}
于 2015-06-20T15:16:33.380 回答
-1

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

于 2013-01-03T04:43:08.250 回答