0

我正在尝试更改 DataGridView 中空行的颜色

foreach (DataGridViewRow r in dgv1.Rows)  
if (r.Value.ToString() == "")
//if (r.Cells.Value.ToString() == "") - also trying
r.DefaultCellStyle.BackColor = Color.WhiteSmoke;

但是 Row 没有 Value 的定义,并且 Cells 需要由 Column 指定。
请问我该怎么做?

4

2 回答 2

1

好吧,您需要检查所有Cells. 以下是使用 LINQ 的方法:

foreach(DataGridViewRow r in dgv1.Rows) {
    if(r.Cells.All(c => c.Value.ToString() == string.Empty)) {
        r.DefaultCellStyle.BackColor = Color.WhiteSmoke;
    }
}
于 2012-06-30T15:04:22.853 回答
0
foreach (DataGridViewRow r in dgv1.Rows)  
if (r.Cells["YourImportantFieldNameLikeID"].Value == null)
r.DefaultCellStyle.BackColor = Color.WhiteSmoke;

如果您不想看到最后一个空行,可以使用以下命令禁用它:

DataGridViewName.AllowUserToAddRows = False;
于 2012-06-30T15:05:30.627 回答