Vb.net DataGridViewCell背景颜色更改而不丢失焦点

Vb.net DataGridViewCell背景颜色更改而不丢失焦点,vb.net,.net-3.5,datagridview,Vb.net,.net 3.5,Datagridview,在VB.Net 3.5中,是否可以将DataGridViewCell(未绑定)的颜色更改为不同的颜色,并在失去焦点或离开单元格之前使单元格发生明显的更改?我有一个正在运行的计时器,可以查询当前的数据,我希望颜色立即改变,而不是在用户离开单元格后 我尝试了DataGridView.Refresh和Me.Refresh,但没有结果 我做错了什么?(以下是我用来更改背景的代码) “” ''将传递的单元格背景设置或清除为红色 ''' ''要更改的单元格的Datagridview列索引 ''要更改的单元

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

我尝试了DataGridView.Refresh和Me.Refresh,但没有结果

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

“”
''将传递的单元格背景设置或清除为红色
''' 
''要更改的单元格的Datagridview列索引
''要更改的单元格的Datagridview行索引
''指示单元格应为红色、True还是空、False
专用子错误_单元格(ByVal ColumnNumber为整数,ByVal RowNumber为整数,ByVal[Error]为布尔值)
如果[错误]那么
Me.dgvMain.Rows(RowNumber).Cells(ColumnNumber).Style.BackColor=Color.Red
其他的
Me.dgvMain.Rows(RowNumber).Cells(ColumnNumber).Style.BackColor=Color.Empty
如果结束
Me.dgvMain.Refresh()
我
端接头

首先,您必须解释行何时使用不同的颜色绘制,例如在我的示例中(当用户单击CheckBoxCell时) 然后,尝试使用不同的事件,如CellLeave或CellContentClick(作为我的示例): 最后,玩代码

void updateCellStyle_DataGridViewCellEventArgs(object sender, DataGridViewCellEventArgs e){
int index = e.RowIndex;
if (index == -1)
 return;
else {
DataGridView dgv = sender as DataGridView;
int vCHK = e.ColumnIndex; //this is the checkbox column
if (vCHK != 0)
 return;
else {
 DataGridViewCheckBoxCell temp = (DataGridViewCheckBoxCell)dgv.Rows[index].Cells[0];
 if ((bool)temp.EditedFormattedValue == true) {
DataGridViewTextBoxCell xrow = (DataGridViewTextBoxCell)dgv.Rows[index].Cells[3];
xrow.OwningRow.DefaultCellStyle.BackColor = Color.Wheat;
 /*
 other operations 
 */
 }
 else {
 temp.OwningRow.DefaultCellStyle.BackColor = Color.White;
 /*
 other operations 
 */
 }

我决定只更改当前单元格的背景色,并开始使用DataGridViewCell的EditControl

为了捕获这一点,我必须获取DataGridView事件“EditControlShowing”上的EditControl(键入DataGridViewTextBoxedTitingContorl),并将cells EditControl与本地表单变量关联,然后向TextChagned事件添加一个处理程序(因为单元格TextChanged直到编辑完成后才会发生)。(第一程序)

这样做之后,我可以通过更改EditControls的背景色来更改单元格的颜色,这会立即更改。(第二程序)

我必须释放DataGridViews CellEndEdit事件上的EditControl,以便重新使用我的私有变量。(第三程序)

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

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中的很多步骤都被删除了,否则,如果可能,它们将是内联的。

当用户输入无效的零件号时,我正在用不同的颜色绘制行。在他们继续做其他事情之前,细胞应该改变颜色,告知他们没有使用正确的部分。我已经断断续续地玩了几天了,所以我想我应该问问你们。
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