【问题标题】:DataGridViewCell Background Color Change Without Losing FocusDataGridViewCell 背景颜色变化不失焦
【发布时间】:2009-09-16 15:34:37
【问题描述】:

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

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

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

''' <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

【问题讨论】:

    标签: vb.net .net-3.5 datagridview


    【解决方案1】:

    首先,您必须解释自己何时将使用不同的颜色绘制行,例如在我的情况下(当用户单击 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 
     */
     }
    

    【讨论】:

    • 当用户输入无效的零件编号时,我正在为该行绘制不同的颜色。在他们继续做其他事情之前,单元格应该改变颜色以通知他们他们没有使用正确的部分。我断断续续地玩了几天,所以我想我会问你们。
    【解决方案2】:

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

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

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

    我必须在 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 中的许多步骤已被删除以保护无辜者,否则,它们将尽可能内联。

    【讨论】:

      猜你喜欢
      • 2017-04-14
      • 2021-10-02
      • 2014-09-14
      • 1970-01-01
      • 2021-02-13
      • 1970-01-01
      • 2012-08-08
      • 2017-12-10
      • 2021-11-10
      相关资源
      最近更新 更多