开发者

Datagridview checkboxcolumn validation

In a DatagridView I have databound checkboxcolumns. But if I check or uncheck multiple checkboxes, not all changes are saved. (It doesn't trigger the property Set method on all, maybe on every 2nd). However if I after each checkbox-click, click on another cell (column) before the next checkbox, then all actions will trigger the Set methods. So it seems the cell validation does not work on a per-cell basis, but on a per column basis (For the checkboxcolumn). So how do you solve this?开发者_StackOverflow中文版


Found the answer: DataGridView with CheckBox cell problem

But probably easier alternative is to put in the save button method:

DataGridView.EndEdit();


It is not really clear what you have tried and what doesn't work. I assume you have a Winform application and use a SqlDataSource to bind a SQL-Server table to your grid. There is a boolean(bit) field in your database, hence the grid automatically generates a DataGridViewCheckBoxColumn. You are trying to save all changes that the user has made when he clicks a save-button.

Are all of these guesses correct?

All you have to do is to update your dataset/datatable with the dataadapter.

Private Sub BtnSaveChanges_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSaveChanges.Click
    Me.FooTableAdapter.Update(DataSet1.Foo)
End Sub

MSD-Example: http://msdn.microsoft.com/en-use/library/fbk67b6z.aspx

If changes should be saved directly to the database, you can handle the CurrentItemChanged-event of the BindingSource.

Private Sub FooBindingSource_CurrentItemChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles FooBindingSource.CurrentItemChanged
     Dim thisDataRow As DataRow = DirectCast(DirectCast(sender, BindingSource).Current, DataRowView).Row
     If thisDataRow.RowState = DataRowState.Modified Then
         Me.FooTableAdapter.Update(thisDataRow)
     End If
End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜