【发布时间】:2012-07-25 04:25:00
【问题描述】:
我有一个 VB.NET 2010 应用程序,它将数据从 SQL Server 加载到 datagridview 到 adapter.fill()。当我更新数据库时它工作正常,但我的问题是当我使用数据视图根据组合框选择过滤数据时,方法 adapter.update(table) 不起作用!
是否可以在应用过滤器的情况下执行此操作?
Private Sub cmbDepartment_SelectedIndexChanged(...)
Handles cmbDepartment.SelectedIndexChanged
Dim filter As String
Try
lblDepartmentId.Text = ds.Tables("department").Rows(cmbDepartment.SelectedIndex)(0)
filter = "dprtId = " & lblDepartmentId.Text
dvSection = New DataView(ds.Tables("section"), filter, "", DataViewRowState.CurrentRows)
table = dvSection.ToTable
dgvSections.DataSource = table
Catch ex As Exception
MsgBox(Err.Description)
End Try
End Sub
Private Sub btnSaveDepartment_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles btnSaveDepartment.Click
Dim cbDep As SqlCommandBuilder
Dim numRows As Integer
Try cbDep = New SqlCommandBuilder(daDep)
Me.Validate() numRows = daDep.Update(ds.Tables("section"))
MsgBox(numRows & " Rows affected.")
Catch ex As Exception
MsgBox(Err.Description)
End Try
End Sub
【问题讨论】:
-
请展示您的代码并尝试更清楚地解释您要做什么。特别是 - 你在哪里应用过滤器?您希望更新调用做什么?听起来您想要更新以检索数据 - 这就是 fill 的用途。我尝试将过滤器应用于绑定源,它工作正常,仍然允许我使用 update 来更新数据库。
-
亲爱的大卫先生,非常感谢您的回复。我会说清楚的。我有部门和部门。在组合框中,我有部门列表,这些部分将显示在 datagridview 中。过滤和更新见以下代码:
-
私人子 cmbDepartment_SelectedIndexChanged(...) 处理 cmbDepartment.SelectedIndexChanged 暗淡过滤器作为字符串尝试 lblDepartmentId.Text = ds.Tables("department").Rows(cmbDepartment.SelectedIndex)(0) filter = " dprtId = " & lblDepartmentId.Text dvSection = New DataView(ds.Tables("section"), filter, "", DataViewRowState.CurrentRows) table = dvSection.ToTable dgvSections.DataSource = table Catch ex As Exception MsgBox(Err.Description)结束尝试结束子
-
Private Sub btnSaveDepartment_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 处理 btnSaveDepartment.Click 将 cbDep 调暗为 SqlCommandBuilder 将 numRows 调暗为整数 Try cbDep = New SqlCommandBuilder(daDep) Me.Validate( ) numRows = daDep.Update(ds.Tables("section")) MsgBox(numRows & " Rows affected.") Catch ex As Exception MsgBox(Err.Description) End Try End Sub
-
我在下面给出了答案 - 在 C# 中对其进行了测试,但在 VB.Net 中应该可以正常工作。此外,当您提供代码时,您可以编辑自己的问题,因此请将代码放在问题中。这使它更容易阅读。
标签: database datagridview filter dataview