我正在使用 C#、Winforms 和 .Net 3.5
我的表单有一个自定义DataGridView
(双缓冲以防止在我的单元格格式化事件期间闪烁,如此处所示)。当我执行数据库搜索时,我将结果数据集绑定到datagridview
.
我CellFormatting
根据数据处理事件以将行绘制成某种颜色。
我的 DataGridView 代码:
resultsGridView.DataSource = results.DefaultViewManager.DataSet.Tables[0];
resultsGridView.AlternatingRowsDefaultCellStyle.BackColor = Color.AliceBlue;
resultsGridView.BorderStyle = BorderStyle.Fixed3D;
resultsGridView.CellFormatting += new DataGridViewCellFormattingEventHandler(resultsGridView_CellFormatting);
我的 CellFormatting 代码:
void resultsGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
int rowIndex = e.RowIndex;
DataGridViewRow therow = resultsGridView.Rows[rowIndex];
if ((bool)therow.Cells["Sealed"].Value == true)
{
therow.DefaultCellStyle.BackColor = Color.Pink;
}
if (therow.Cells["Database"].Value as string == "PNG")
{
therow.DefaultCellStyle.BackColor = Color.LightGreen;
}
}
一切都很好,除了当我处理 CellFormatting 时,整个表单的 Paint 事件似乎被关闭了。光标在文本框中停止闪烁,表单的菜单条如下所示:
顶部是搜索前,底部是搜索后。在我将鼠标悬停在菜单项所在的位置之前,菜单栏不会重绘,然后当我将鼠标移出菜单栏时,要突出显示的最后一个项目将保持不变。移动表单似乎会导致它重新绘制,但问题仍然存在。
注释掉resultsGridView.CellFormatting
datagridview 代码中的行完全解决了这个问题。
我画错了单元格,还是我需要处理其他事情?