Я условно делаю цвет фона некоторых клеток зеленым, желтым или красным. Это работает. Однако каждый раз, когда я сортирую по конкретному столбцу, ячейки временно возвращаются на белый фон, прежде чем одно из этих событий стреляет и повторно приобретает фоновые цвета. < /P>
Paint()
PrePaint()
CellPainting()
< /code>
Это показывает, что DataGridView переехал дважды < /p>
Мне не нравится, что он дважды рисует ячейки. Похоже, что должен быть способ сделать это правильно, чтобы раз лучшую производительность и эстетику. Каждый из них работает, но плохо, так как фон для ячейки все еще становится белым, прежде чем они стреляют. < /P>
private void dgv_cons_avail_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
// Stores the value of the current cell.
string str_cell_value;
// If this the hour column then...
if (e.ColumnIndex == 0 || e.RowIndex == -1)
{
// No color highlighting is needed.
return;
}
// Stores the value of the current cell.
str_cell_value = dgv_cons_avail.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
// If no users are available at the current timeslot then...
if (str_cell_value == "No one")
{
// Changes the background color of the cell to red if no one is available.
dgv_cons_avail.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.FromArgb(255, 255, 199, 206);
}
// If all users are available at the current timeslot then...
else if (str_cell_value == "Everyone")
{
// Changes the background color of the cell to green if everyone is available.
dgv_cons_avail.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.FromArgb(255, 198, 238, 206);
}
// If some of the users are available at the curren timeslot then...
else
{
// Changes the background color of the cell to yellow if some users are
// available.
dgv_cons_avail.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.FromArgb(255, 255, 235, 156);
}
}
private void dgv_cons_avail_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
// Stores the value of the current cell.
string str_cell_value;
for (int i = 1; i < dgv_cons_avail.ColumnCount; i++)
{
// Stores the value of the current cell.
str_cell_value = dgv_cons_avail.Rows[e.RowIndex].Cells.Value.ToString();
// If no users are available at the current timeslot then...
if (str_cell_value == "No one")
{
// Changes the background color of the cell to red if no one is available.
dgv_cons_avail.Rows[e.RowIndex].Cells.Style.BackColor = Color.FromArgb(255, 255, 199, 206);
}
// If all users are available at the current timeslot then...
else if (str_cell_value == "Everyone")
{
// Changes the background color of the cell to green if everyone is available.
dgv_cons_avail.Rows[e.RowIndex].Cells.Style.BackColor = Color.FromArgb(255, 198, 238, 206);
}
// If some of the users are available at the curren timeslot then...
else
{
// Changes the background color of the cell to yellow if some users are
// available.
dgv_cons_avail.Rows[e.RowIndex].Cells.Style.BackColor = Color.FromArgb(255, 255, 235, 156);
}
}
}
private void dgv_cons_avail_Paint(object sender, PaintEventArgs e)
{
// Stores the value of the current cell.
string str_cell_value;
for (int int_row = 0; int_row < dgv_cons_avail.RowCount; int_row++)
{
for (int int_col = 1; int_col < dgv_cons_avail.ColumnCount; int_col++)
{
// Stores the value of the current cell.
str_cell_value = dgv_cons_avail.Rows[int_row].Cells[int_col].Value.ToString();
// If no users are available at the current timeslot then...
if (str_cell_value == "No one")
{
// Changes the background color of the cell to red if no one is available.
dgv_cons_avail.Rows[int_row].Cells[int_col].Style.BackColor = Color.FromArgb(255, 255, 199, 206);
}
// If all users are available at the current timeslot then...
else if (str_cell_value == "Everyone")
{
// Changes the background color of the cell to green if everyone is available.
dgv_cons_avail.Rows[int_row].Cells[int_col].Style.BackColor = Color.FromArgb(255, 198, 238, 206);
}
// If some of the users are available at the curren timeslot then...
else
{
// Changes the background color of the cell to yellow if some users are
// available.
dgv_cons_avail.Rows[int_row].Cells[int_col].Style.BackColor = Color.FromArgb(255, 255, 235, 156);
}
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... -when-i-so