Datagridview复选框列验证

发布于 2024-10-13 11:40:39 字数 240 浏览 2 评论 0原文

DatagridView中,我有数据绑定复选框列。但是,如果我选中或取消选中多个复选框,则并非所有更改都会保存。 (它不会全部触发属性 Set 方法,也许每隔两个触发一次)。但是,如果我在每个复选框单击后,单击下一个复选框之前的另一个单元格(列),则所有操作都将触发 Set 方法。因此,单元格验证似乎不是在每个单元格的基础上工作,而是在每列的基础上工作(对于复选框列)。那么如何解决这个问题呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦行七里 2024-10-20 11:40:39

找到答案: DataGridView with CheckBox cell Problem

但可能更简单的替代方法是放入保存按钮方法:

DataGridView.EndEdit();

Found the answer: DataGridView with CheckBox cell problem

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

DataGridView.EndEdit();
沫雨熙 2024-10-20 11:40:39

目前还不清楚您尝试了什么以及什么不起作用。我假设您有一个 Winform 应用程序并使用 SqlDataSource 将 SQL-Server 表绑定到网格。数据库中有一个布尔(位)字段,因此网格会自动生成一个 DataGridViewCheckBoxColumn。您试图保存用户单击保存按钮时所做的所有更改。

这些猜测都正确吗?

您所要做的就是使用数据适配器更新数据集/数据表。

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-示例: http://msdn.microsoft.com/en-use/library /fbk67b6z.aspx

如果应将更改直接保存到数据库,您可以处理 BindingSource 的 CurrentItemChanged 事件

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文