У меня проблема с моим DataGridView
, где я использую метод CellFormatting, чтобы перекрасить строку на основе ее содержимого. В большинстве случаев это нормально работает, пока первая строка в DGV не вернет true - в этом случае каждая строка в DGV окрашена в красный цвет.C# DataGridView Row Coloring
Вот мой код:
private void MyData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridViewRow theRow = MyData.Rows[e.RowIndex];
if (theRow.Cells[4].Value.ToString() == "1")
{
ChangeRowColor(theRow, Color.PaleVioletRed);
}
}
private void ChangeRowColor(DataGridViewRow r, Color c)
{
r.DefaultCellStyle.BackColor = c;
}
Edit: Решение:
private void MyData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridViewRow theRow = MyData.Rows[e.RowIndex];
if ((int)theRow.Cells[4].Value == 1)
{
e.CellStyle.BackColor = Color.PaleVioletRed;
}
}
Спасибо за форматирование для меня, я новичок – Krisisonfire
Добавление 'else {ChangeRowColor (theRow, Color.White);}' после предложения else if? – Pikoh
Можете ли вы предоставить данные образца? – Berkay