0

我想测试 DataGridView 中的行颜色变化,所以我编写了该代码:

dataGridView1.Rows.Add(new object[] { "Uno", "No" });
dataGridView1.Rows.Add(new object[] { "Due", "No" });
dataGridView1.Rows.Add(new object[] { "Tre", "Yes" });
dataGridView1.Rows.Add(new object[] { "Quattro", "No" });
dataGridView1.Rows.Add(new object[] { "Cinque", "Yes" });

private void button1_Click(object sender, EventArgs e)
    {            
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            if (row.Cells[1].Value.ToString() == "Yes")
                row.DefaultCellStyle.ForeColor = Color.Red;
            else
                row.DefaultCellStyle.ForeColor = Color.Green;
        }
    }

所以,有五行两列。但是当我试图改变颜色时,它给了我一个 NullReference 异常,说 row.Cells[1] 值为空。怎么了?

4

4 回答 4

0

通过更改您的颜色,rowsDataGridView可以在DataGridView.CellFormatting 事件中执行此操作,并且您的主要问题是使用String.IsNullOrWhiteSpace 方法

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.ColumnIndex == 1)
    {
        if (string.IsNullOrEmpty(dataGridView1[e.ColumnIndex, e.RowIndex].EditedFormattedValue.ToString()))
        {
            if (dataGridView1[e.ColumnIndex, e.RowIndex].EditedFormattedValue.ToString() == "Yes")
            {
                dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Red;
            }
            else
            {
                dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Green;
            }
        }
        else
            dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Green;
    }
}
于 2013-01-16T12:55:59.077 回答
0

尝试使用 check row.IsNewRow,例如

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    if (row.IsNewRow)
      continue;
    if (row.Cells[1].Value.ToString() == "Yes")
      row.DefaultCellStyle.ForeColor = Color.Red;
    else
      row.DefaultCellStyle.ForeColor = Color.Green;
}
于 2013-01-16T13:22:26.363 回答
0

删除 if 中的“toString()”,它正在工作。值正在返回一个字符串;)

更新:使用这个。它将对象转换为字符串

 if ((string)row.Cells[1].Value == "Yes")
                row.DefaultCellStyle.ForeColor = Color.Red;
            else
                row.DefaultCellStyle.ForeColor = Color.Green;
于 2013-01-16T12:58:11.463 回答
0

我认为您的方法的问题是,一旦将行连接到网格,您就无法更改行的设计。

但是要实现您想要的,您可以使用 RowPrePaint 事件。

即来自我的一个应用程序:

private void tSEventsDataGridView_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
    if (e.RowIndex >= 0)
    {
        //create new CellStyle
        DataGridViewCellStyle cellStyle = new DataGridViewCellStyle();

        //if we have a value and that value is greater than the MaximumHoursWarning setting
        object cellValue = tSEventsDataGridView[eventTimeDataGridViewTextBoxColumn.Index, e.RowIndex].Value;
        if (cellValue is double
            && (double) cellValue > Timesheets.Properties.Settings.Default.MaximumHoursWarning)
            cellStyle.BackColor = Color.FromArgb(255, 220, 220);  //change the color to light red
        else if (e.RowIndex % 2 == 0)
            cellStyle.BackColor = Color.FromArgb(240, 240, 240);          //else keep the alternating background
        else
            cellStyle.BackColor = Color.White;

        //set the CellStyle for this row.
        tSEventsDataGridView.Rows[e.RowIndex].DefaultCellStyle = cellStyle;
    }
}
于 2013-01-16T12:58:38.080 回答