【发布时间】:2010-12-22 04:09:06
【问题描述】:
我正在使用 C#、Winforms 和 .Net 3.5
我的表单有一个自定义DataGridView(双缓冲以防止在我的单元格格式化事件期间闪烁,as seen here)。当我执行数据库搜索时,我将结果数据集绑定到 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 事件似乎被关闭了。光标在文本框中停止闪烁,表单的菜单条如下所示:
顶部在搜索之前,底部在搜索之后。在我将鼠标悬停在菜单项所在的位置之前,菜单栏不会重绘,然后当我将鼠标移出菜单栏时,要突出显示的最后一个项目将保持不变。移动表单似乎会导致它重新绘制,但问题仍然存在。
注释掉 datagridview 代码中的resultsGridView.CellFormatting 行可以完全解决问题。
是我画错了单元格,还是我需要处理其他问题?
【问题讨论】:
标签: c# winforms events datagridview paint