Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataGridView CellFormatting事件阻止表单绘制_C#_Winforms_Events_Datagridview_Paint - Fatal编程技术网

C# DataGridView CellFormatting事件阻止表单绘制

C# DataGridView CellFormatting事件阻止表单绘制,c#,winforms,events,datagridview,paint,C#,Winforms,Events,Datagridview,Paint,我正在使用C#、Winforms和.NET3.5 我的表单有一个自定义的DataGridView(双缓冲区以防止在我的cellformatting事件中闪烁,)。当我执行数据库搜索时,我将结果数据集绑定到datagridview 我处理CellFormatting事件,根据行的数据将行绘制成特定的颜色 我的DataGridView代码: resultsGridView.DataSource = results.DefaultViewManager.DataSet.Tables[0]; resul

我正在使用C#、Winforms和.NET3.5

我的表单有一个自定义的
DataGridView
(双缓冲区以防止在我的cellformatting事件中闪烁,)。当我执行数据库搜索时,我将结果数据集绑定到
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);
我的手机格式代码:

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时,整个表单的绘制事件似乎都被关闭了。光标在文本框中停止闪烁,窗体的菜单提示如下所示:

顶部在搜索之前,底部在搜索之后。菜单栏将不会重新绘制,直到我将鼠标移到菜单项所在的位置,然后当我将鼠标移出菜单栏时,最后一个高亮显示的项将保持这种状态。移动表单似乎会导致它重新绘制,但问题仍然存在

注释掉datagridview代码中的
resultsGridView.CellFormatting
行完全解决了这个问题


我是否绘制了错误的单元格,或者是否需要处理其他问题?

您可能导致此事件内部出现异常。我不确定处理是如何定义的,但用try-catch围绕代码将是第一步

try 
{
   int rowIndex = e.RowIndex;
   ....   
}
catch(Exception ex)
{
    System.Diagnostics.Trace.Error(ex.message);
}

再看看,我认为
therow.Cells[“Sealed”]
不起作用。尝试类似于
therow.Cells[“dataGridViewTextBoxColumn2”]
的方法。单元格由列名编制索引,而不是DataPropertyName

“密封”部分不是数据属性,而是数据库中的布尔条件。我正在做的程序会搜索一个名字,可以显示我们部门的案件档案是否已经封存。没有例外。我在格式化事件的开始添加了
Systems.diagnostics.Trace.WriteLine(“start”)
,它运行了很多次,即使在单元格已经显示并且没有其他事情发生之后。但是“Sealed”是Dgv列对象的名称吗?我想你应该设置
e.CellStyle.BackColor
,不是
theRow.DefaultCellStyle.BackColor
。你知道了-改变单元格的绘制方式是个窍门。更改为
e.CellStyle.BackColor
可以修复此问题。