如何刷新CheckBox上的绑定DataGridView单击

时间:2014-02-26 16:09:22

标签: vb.net datagridview datatable datasource rowfilter

目标

我需要一种方法,通过单击绑定CheckBox中的DataGridView将项目设置为已完成。完成后,由于DataGridView的行过滤器,DataView不再显示该项目。


说明

我有DataGridView绑定到DataView

Private Sub LoadLispImprovements()
    Private ViewLispImprovements As New DataView
    ViewLispImprovements.Table = Programs.Tables("dtLispImprovements")
    ViewLispImprovements.RowFilter = "Completed=0"
    Lisp_dgvImprovements.DataSource = ViewLispImprovements
End Sub

DataView的表格设置为以下DataTable

DataTable showing Primary Key and Boolean Value (in red)

DataTable将其主键设置为正确的字段。 Completed字段有一个红色方块,只是为了向大家展示我将其设置为布尔值。

看看我的DataGridView

DataGridView showing the result of the bound items

在这里,我点击复选框,该项目应该被过滤掉(因为未选中复选框 ShowAll


问题

当我点击CheckBox时,该项目会保留在DataGridView中。为了将其过滤掉,我必须选择另一行。看看:

Improvement is checked, but remains in the Dgv

当我改变行时,它最终会消失:

Improvement has been filtered out but only when I tried selecting another row

在选中完成的复选框后,如何让它立即删除?

P.S:是的我有CurrentCellDirtyStateChanged事件处理程序。

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

1 个答案:

答案 0 :(得分:5)

您可以通过在更改单元格值时结束DataRow的编辑来实现此目的。

Private Sub Lisp_dgvImprovements_CurrentCellDirtyStateChanged(sender As Object, e As System.EventArgs) Handles Lisp_dgvImprovements.CurrentCellDirtyStateChanged
    If (Me.Lisp_dgvImprovements.IsCurrentCellDirty) Then
        Me.Lisp_dgvImprovements.CommitEdit(DataGridViewDataErrorContexts.Commit)
    End If
End Sub

Private Sub Lisp_dgvImprovements_CellValueChanged(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Lisp_dgvImprovements.CellValueChanged
    '                    |-- The index of boolean DataGridViewColumn
    If ((e.ColumnIndex = 0) AndAlso (e.RowIndex > -1)) Then
        With Me.Lisp_dgvImprovements.Rows(e.RowIndex)
            If (Not .IsNewRow) Then
                With DirectCast(.DataBoundItem, DataRowView).Row
                    '     |-- The index of boolean DataColumn
                    .Item(0) = True
                    .EndEdit()
                End With
            End If
        End With
    End If
End Sub