【发布时间】:2012-03-20 15:23:39
【问题描述】:
我想根据加载时的特定条件更改 DGV 行的背景颜色,即使在 Windows 窗体中也是如此。但我看不到任何 DGV 行的颜色变化。谁能告诉我如何解决这个问题?
private void frmSecondaryPumps_Load(object sender, EventArgs e)
{
try
{
DataTable dt = DeviceData.BindData("SECONDARY_PUMPS".ToUpper());
dataGridView1.DataSource = dt;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewColumn column in dataGridView1.Columns)
{
if (row.Cells[column.Name] != null)
{
if (row.Cells[column.Name].Value.ToString() == "ON")
row.DefaultCellStyle.BackColor = System.Drawing.Color.Green;
if (row.Cells[column.Name].Value.ToString() == "OFF")
row.DefaultCellStyle.BackColor = System.Drawing.Color.Red;
}
}
}
dataGridView1.Refresh();
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
【问题讨论】:
-
为什么不设置
BackColor而不是ForeColor -
我也试过了,但是在 DGV 的任何一行中我都看不到行颜色有任何变化
标签: c# .net winforms datagridview