C#DataGridView行着色
问题描述:
我遇到了我的DataGridView
问题,其中我使用CellFormatting方法根据其内容为行重新着色。在大多数情况下,这似乎可以正常工作,直到DGV中的第一行返回true为止 - 在这种情况下,DGV中的每一行都显示为红色。C#DataGridView行着色
这里是我的代码:
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。
感谢您为我设置格式,我是一个新手 – Krisisonfire
在else if子句之后添加'else {ChangeRowColor(theRow,Color.White);}'? – Pikoh
你能提供你的样本数据吗? – Berkay