【问题标题】:Datagridview control's forecolor only changes after the event handler runs AND another cell is clickedDatagridview 控件的前景色仅在事件处理程序运行并单击另一个单元格后更改
【发布时间】:2012-01-06 18:13:38
【问题描述】:

我想在双击单元格时更改 datagridview 中单元格的颜色。我添加了一个 CellDoubleClick 处理程序,该处理程序在双击单元格后正确触发:

Private Sub myDataGridView_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles myDataGridView.CellDoubleClick
    myDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.ForeColor = Color.Red
    myDataGridView.Refresh()  'I added this to try to fix the problem
    Application.DoEvents()    'I added this to try to fix the problem
end sub

但在事件“触发”后,UI 不会立即更改单元格文本的颜色。相反,如果我单击一个 不同 单元格,那么 Visual Studio 2010 会将 original 单元格更改为红色(就像处理程序所说的那样)。

似乎每次单击不同的单元格时都会进行某种 UI 刷新/UI 重绘。这个对吗?有没有办法以编程方式刷新 UI。许多 stackoverflow 帖子建议 .refresh 但这不起作用。

【问题讨论】:

    标签: .net winforms datagridview


    【解决方案1】:

    这是因为当您双击一个单元格时,它仍然处于选中状态,并且使用了属性 .SelectionForeColor 和 .SelectionBackColor。只有当您单击另一个单元格时,它才会被取消选择并开始使用 .ForeColor 和 .BackColor。您可以通过更改 .SelectionForeColor 属性或在之后立即设置 .Selected = False 来立即更新它。

    【讨论】:

    • 我也想通了!谢谢!
    猜你喜欢
    • 2012-09-27
    • 1970-01-01
    • 2013-08-20
    • 2020-02-15
    • 1970-01-01
    • 1970-01-01
    • 2013-04-12
    • 2010-12-15
    • 2016-05-27
    相关资源
    最近更新 更多