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

したがって、5つの行と2つの列があります。しかし、色を変更しようとすると、row.Cells[1]の値がnullであるというNullReference例外が発生しました。どうしたの?

4

4 に答える 4

0

の色を変更することrowsで、 DataGridView.CellFormattingイベントDataGridViewでこれを行うことができます。主な問題は、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

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イベントを使用できます。

つまり、私のアプリケーションの1つから:

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 に答える
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 に答える