【发布时间】:2014-10-08 12:16:52
【问题描述】:
foreach (DataGridViewRow row in dataGridView1.Rows)
{
string kesim = Convert.ToString(row.Cells["kesim"].Value);
if (kesim == "True")
{
dataGridView1.Rows[row.Index].Cells["kesim"].Style.BackColor = Color.Green;
dataGridView1.Rows[row.Index].Cells["kesim"].ReadOnly = true;
dataGridView1["kesim", row.Index].ReadOnly = true;
dataGridView1["kesim", row.Index].Style.BackColor = Color.Green;
}
else
{
dataGridView1.Rows[row.Index].Cells["kesim"].Style.BackColor = Color.Red;
dataGridView1.Rows[row.Index].Cells["kesim"].ReadOnly = true;
dataGridView1["kesim", row.Index].ReadOnly = true;
dataGridView1["kesim", row.Index].Style.BackColor = Color.Red;
}
string torna = Convert.ToString(row.Cells["torna"].Value);
if (torna == "True")
{
dataGridView1.Rows[row.Index].Cells["torna"].Style.BackColor = Color.Green;
dataGridView1.Rows[row.Index].Cells["torna"].ReadOnly = true;
dataGridView1["torna", row.Index].ReadOnly = true;
dataGridView1["torna", row.Index].Style.BackColor = Color.Green;
}
else
{
dataGridView1.Rows[row.Index].Cells["torna"].Style.BackColor = Color.Red;
dataGridView1.Rows[row.Index].Cells["torna"].ReadOnly = true;
dataGridView1["torna", row.Index].ReadOnly = true;
dataGridView1["torna", row.Index].Style.BackColor = Color.Red;
}
}
我的第一行;
我的第二排;
如果单元格值为真,我想将背景颜色更改为绿色,否则为红色。
我从这个链接获取了代码;
Using a dynamic list of Custom Object and can't dynamically change dataGrid's cells properties
请帮助我的问题
【问题讨论】:
-
使用CellFormatting 事件。
-
你是认真的吗?我已经在使用这个了。我的代码在我的问题上
-
您没有向我们展示您在哪里使用此代码。不要循环遍历行。只需格式化从 DataGridViewCellFormattingEventArgs 参数获得的单元格。
-
不,您的代码中没有包含事件标头。
-
对不起,我怎么能用 cellformatting 做到这一点?
标签: c# datagridview formatting cell