DataGridViewCell 背景颜色变化而不会失去焦点 [英] DataGridViewCell Background Color Change Without Losing Focus

查看:39
本文介绍了DataGridViewCell 背景颜色变化而不会失去焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 VB .Net 3.5 中,是否可以将 DataGridViewCell(未绑定)的颜色更改为不同的颜色,并在失去焦点或离开单元格之前让单元格发生明显变化?我有一个计时器,该计时器正在使用现有数据运行查询,我希望颜色立即更改,而不是在用户离开单元格后更改.

In VB .Net 3.5, is it possible to change the color of a DataGridViewCell (unbound) to a different color and have the cell visibly change before losing focus or leaving the cell? I have a timer that's running that queries with the data present and I'd like for the colors to change immediately instead of after the user leaves the cell.

我尝试过 DataGridView.Refresh 和 Me.Refresh,但没有得到结果.

I've tried DataGridView.Refresh and Me.Refresh and don't get results.

我做错了什么?(下面是我用来改变背景的代码)

What am I doing wrong? (Below is the code I use to change the background)

''' <summary>
''' Sets or clears the passed cells background to Red
''' </summary>
''' <param name="ColumnNumber">Datagridview column index of the cell to be changed</param>
''' <param name="RowNumber">Datagridview row index of the cell to be changed</param>
''' <param name="Error">Indicate whether the cell should be red, <c>True</c>, or empty, <c>False</c></param>
Private Sub Error_Cell(ByVal ColumnNumber As Integer, ByVal RowNumber As Integer, ByVal [Error] As Boolean)
    If [Error] Then
        Me.dgvMain.Rows(RowNumber).Cells(ColumnNumber).Style.BackColor = Color.Red
    Else
        Me.dgvMain.Rows(RowNumber).Cells(ColumnNumber).Style.BackColor = Color.Empty
    End If
    Me.dgvMain.Refresh()
    Me.Refresh()
End Sub

推荐答案

我决定只需要改变当前单元格的背景颜色并开始使用 DataGridViewCell 的 EditControl.

I decided that only the current cells background color need to change and began using the EditControl of the DataGridViewCell.

为了捕获这一点,我必须在 DataGridView 事件EditControlShowing"上获取 EditControl(类型 DataGridViewTextBoxEditingContorl)并将单元格 EditControl 与本地表单变量相关联,然后向 TextChagned 事件添加一个处理程序(因为 Cells TextChanged直到编辑完成后才会发生).(第一道工序).

In order to capture this, I had to grab the EditControl (Type DataGridViewTextBoxEditingContorl) on the DataGridView event "EditControlShowing" and associate the cells EditControl with a local form variable, then add a handler to the TextChagned event (since the Cells TextChanged doesn't occur until after editing is complete). (First procedure).

这样做后,我可以通过更改立即更改的 EditControls BackColor 来更改单元格的颜色.(第二道工序)

After doing so, I'm able to change the color of the cell by changing the EditControls BackColor, which is changed immediately. (Second procedure)

我必须在 DataGridViews CellEndEdit 事件上释放 EditControl 才能重新使用我的私有变量.(第三道工序)

I have to release the EditControl on the DataGridViews CellEndEdit event in order to re-use my private variable. (Third procedure)

由于使用的变化,我还没有测试过尝试更改行,但它似乎工作得很好.我希望这可以帮助任何有类似问题的人.如果有更有效的方法,请告诉我!

I haven't tested attempting to change the row, due to a change in usage, but it seems to work just fine. I hope this helps anyone with a similar issue. If there are more efficient ways of doing this, please let me know!

Private EditingControl As DataGridViewTextBoxEditingControl

Private Sub dgvMain_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgvMain.EditingControlShowing
    If TypeOf (e.Control) Is DataGridViewTextBoxEditingControl Then
        EditingControl = DirectCast(e.Control, DataGridViewTextBoxEditingControl)
        If DirectCast(e.Control, DataGridViewTextBoxEditingControl).EditingControlDataGridView.CurrentCell.OwningColumn Is PartNumber Then
            AddHandler EditingControl.TextChanged, AddressOf EditingControl_TextChanged
        End If
    End If
End Sub

Private Sub Error_Cell(ByVal [Error] As Boolean)
    If [Error] Then
        If EditingControl IsNot Nothing Then EditingControl.BackColor = Color.Red
    Else
        If EditingControl IsNot Nothing Then EditingControl.BackColor = Color.Empty
    End If
    Me.dgvMain.Refresh()
End Sub



Private Sub dgvMain_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvMain.CellEndEdit
    If EditingControl IsNot Nothing Then
        RemoveHandler EditingControl.TextChanged, AddressOf EditingControl_TextChanged
    End If

    EditingControl = Nothing

End Sub

注意:我在 If/Then 中的很多步骤已被删除以保护无辜者,否则,它们将尽可能内联.

Note: A lot of the steps that I have inside of the If/Then's have been removed to protect the innocent, otherwise, they would be inline when possible.

这篇关于DataGridViewCell 背景颜色变化而不会失去焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆