C# DataGridView 行着色
C# DataGridView Row Coloring
我的 DataGridView
有问题,我使用 CellFormatting 方法根据行的内容重新着色行。在大多数情况下,这似乎工作正常,直到 DGV 中的第一行 returns true - 在这种情况下,DGV 中的每一行都是红色的。
这是我的代码:
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;
}
编辑:解决方案:
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;
}
}
您应该在事件中将背景色指定给 CellStyle。
我的 DataGridView
有问题,我使用 CellFormatting 方法根据行的内容重新着色行。在大多数情况下,这似乎工作正常,直到 DGV 中的第一行 returns true - 在这种情况下,DGV 中的每一行都是红色的。
这是我的代码:
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;
}
编辑:解决方案:
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;
}
}
您应该在事件中将背景色指定给 CellStyle。