【发布时间】:2016-11-03 12:02:55
【问题描述】:
我的DataGridView 有问题,我使用 CellFormatting 方法根据行的内容重新着色。在大多数情况下,这似乎工作正常,直到 DGV 中的第一行返回 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;
}
}
【问题讨论】:
-
感谢为我格式化,我是新手
-
在 else if 子句之后添加
else { ChangeRowColor(theRow, Color.White);}有帮助吗? -
你能提供你的样本数据吗?
-
您可以运行上面的代码,只要 DGVColumn[4] 在 DGVRow[0] 上的值为“1”,无论值如何,整个网格都会变为红色,但是如果 DGVRow[ 0] == "0" 或任何其他值,只有在 Col[4] 中等于 "1" 的行才会按预期显示为红色。
-
尝试添加我在评论中告诉你的
else部分,并告诉我它是否有所作为
标签: c# datagridview colors row cell-formatting