简体   繁体   中英

Datagridview: how to commit checkbox and and immediately filter by it?

I have a datagridview DataGridView1, bound to a DataTable1BindingSource, with the only writable cell being a checkbox bound to the boolean column "IsSelected". As soon as the user checks the checkbox, I want to show only the row where the checkbox is checked. So i put the following code:

Private Sub DataGridView1_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
    DataGridView1.EndEdit()
    DataTable1BindingSource.Filter = "IsSelected"
End Sub

But it does not work - once the ckeckbox is checked, the datagridview does not show any rows at all.

If I move the statement DataTable1BindingSource.Filter = "IsSelected" to a separate command button,

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    DataTable1BindingSource.Filter = "IsSelected"
End Sub

then upon editing the checkbox and then clicking the button, it works as expected, ie shows only this row.

Why it does not work when it's issued immediately?

Found the reason. After

Sender.EndEdit

, before applying the filter, have to also issue

DataTable1BindingSource.EndEdit

Then it all works as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM