【问题标题】:Toggle CheckBox Cell in DataGridView Programmatically以编程方式切换 DataGridView 中的复选框单元格
【发布时间】:2012-06-05 17:12:51
【问题描述】:

当用户取消选中 datagridview 行的复选框时,显示消息框,如果用户回答是,则删除该行,如果用户回答否,则我需要重新选中复选框。但是,除非我之后单击 datagridview 上的任何位置,否则它实际上并没有检查。有什么想法可以让 dgv 更新吗?

Private Sub DataGridView2_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView2.CellValueChanged

  If e.RowIndex <> -1 Then
    Dim row As DataGridViewRow = DataGridView2.Rows(e.RowIndex)
    If e.ColumnIndex = 0 Then
      If row.Cells(0).Value = False Then
        If MessageBox.Show("Delete view from database.", "Delete", MessageBoxButtons.YesNo) = DialogResult.Yes Then
          DataGridView2.Rows.RemoveAt(e.RowIndex)
        Else
          row.Cells(0).Value = True
          DataGridView2.Invalidate()
        End If
      End If
    End If
  End If
End Sub

Private Sub DataGridView2_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DataGridView2.CurrentCellDirtyStateChanged
  If DataGridView2.IsCurrentCellDirty Then
    DataGridView2.CommitEdit(DataGridViewDataErrorContexts.Commit)
  End If
End Sub

【问题讨论】:

    标签: vb.net winforms


    【解决方案1】:

    请尝试调用RefreshEdit(),而不是Invalidate(),如下所示:

    Else
      row.Cells(0).Value = True
      DataGridView2.RefreshEdit()
    End If
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-25
      • 2015-01-15
      • 2010-11-02
      • 2017-02-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多