我必须更改单元格的背景颜色,当它们的值为字符串或空时,这是我编写的代码,与此处的其他代码类似:
for (int rowIndex = 0; rowIndex < dataGridView1.RowCount; rowIndex++)
{
string conte = dataGridView1.Rows[rowIndex].Cells[7].Value.ToString() ;
if (string.IsNullOrEmpty(conte))
{
// dataGridView1.Rows[rowIndex].Cells[7].Style.BackColor = Color.Orange;
}
else
{ dataGridView1.Rows[rowIndex].Cells[7].Style.BackColor = Color.Orange; }
}
the dataset is complete , show me the datagridview populated and the show me this error:
我怎样才能解决这个问题??还有另一种方式来编写代码吗?
我将使用类似以下内容的方法迭代单元格。
foreach (DataGridViewRow dgRow in dataGridView1.Rows)
{
var cell = dgRow.Cells[7];
if (cell.Value != null) //Check for null reference
{
cell.Style.BackColor = string.IsNullOrEmpty(cell.Value.ToString()) ?
Color.LightCyan :
Color.Orange;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)