【发布时间】:2015-06-16 07:07:57
【问题描述】:
我正在从返回的数据库表中创建一个动态 DataGridView。我需要根据值将一些单元格更改为红色。我为 CellFormatting 事件分配了一个函数,但每次用户单击任何单元格/行时都会调用该函数(这会减慢表单的速度)。
我想只在加载时执行该功能。
我尝试通过循环表格来设置样式,但背面颜色没有改变。我只有在使用 CellFormatting 事件时才能使用它。
我的代码:
this.dgv.CellFormatting += new System.Windows.Forms.DataGridViewCellFormattingEventHandler(this.Dgv_CellFormatting);
在函数中我改变颜色
private void Dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.Value != null)
{
if (e.ColumnIndex == 0)
{
if ((int)e.Value >= 5)
{
e.CellStyle.BackColor = Color.Red;
}
}
}
}
【问题讨论】:
-
请发布您目前拥有的任何代码。
标签: c# winforms datagridview background-color