在DataGridView中手动添加行

时间:2013-01-16 12:50:38

标签: c# datagridview

我想测试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]值为null。怎么了?

4 个答案:

答案 0 :(得分:0)

通过更改rows DataGridView的颜色,您可以在DataGridView.CellFormatting Event中执行此操作,而主要问题是使用String.IsNullOrWhiteSpace Method

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

答案 1 :(得分:0)

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

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

 if ((string)row.Cells[1].Value == "Yes")
                row.DefaultCellStyle.ForeColor = Color.Red;
            else
                row.DefaultCellStyle.ForeColor = Color.Green;

答案 2 :(得分: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;
    }
}

答案 3 :(得分: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;
}