Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Refresh a Bound DataGridView On CheckBox Click

Goal

I need a way to set items as Completed by clicking on a CheckBox in my bound DataGridView. Once that is done, the item is no longer seen from the DataGridView due to a DataView's row filter.


Explanation

I have a DataGridView that is bound to a DataView.

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

This DataView's table was set to the following DataTable:

DataTable showing Primary Key and Boolean Value (in red)

This DataTable has its Primary Key set to the correct field. The Completed field has a red square just to show you guys that I set it as a boolean value.

Take a look at my DataGridView:

DataGridView showing the result of the bound items

Here is where I would click on the checkbox and the item should get filtered out (because the checkbox ShowAll is not checked)


Problem

When I click on the CheckBox , the item stays in the DataGridView. In order for it to get filtered out, I must select another row. Take a look:

Improvement is checked, but remains in the Dgv

And when I change rows, it finally goes away:

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

How do I get it to remove itself immediatly after I checked the completed checkbox?

P.S: Yes I have the CurrentCellDirtyStateChanged event handler.

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
like image 958
Alex Avatar asked Feb 26 '14 16:02

Alex


1 Answers

You can achieve this by ending the edit of the DataRow when the value of the cell is changed.

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
like image 171
Bjørn-Roger Kringsjå Avatar answered Oct 14 '22 19:10

Bjørn-Roger Kringsjå