Фон ячейки в DataGridView - PullRequest
       14

Фон ячейки в DataGridView

1 голос
/ 08 января 2010

Я немного поигрался с сетевыми представлениями, и возник вопрос. -> Не меняет фон ячейки, работает из события CellFormatting. Я пробовал это:

private void dataGridView1_CellFormatting(object sender, dataGridViewCellFormattingEventArgs e)
{
    if (dataGridView1.Columns[e.ColumnIndex].Name.Equals(dnsList.First<String>()))
    {
        DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
        DataGridViewCell cell = row.Cells[e.ColumnIndex];
        cell.Style.BackColor = Color.FromArgb(194, 235, 211);
    }
}

, который работает отлично, тогда как это:

private void ApplyColoring()
{
    if (dataGridView1.DataSource != null)
    {
        foreach (DataGridViewRow dataGridRow in dataGridView1.Rows)
        {
            DataGridViewCell cell = dataGridRow.Cells[dnsList.First<String>()];
            cell.Style.BackColor = Color.FromArgb(194, 235, 211);
        }
    }
}

Отладка говорит мне, что все звучит нормально, с нулевой ссылкой или любым другим исключением Любые советы?

Ответы [ 2 ]

1 голос
/ 08 января 2010

Вы должны выполнить раскраску в событии CellPainting вместо CellFormatting, которое предназначено для форматирования значения ячейки

0 голосов
/ 08 января 2010

Я предлагаю вызывать ваш метод из события DataBindingComplete сетки. Вызов из BackgroundWorker, вероятно, слишком рано, и DataGridView.Rows еще не инициализированы.


private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
      ApplyColoring();
}

...