【问题标题】:Update DataGridView with dataview使用数据视图更新 DataGridView
【发布时间】:2012-07-25 04:25:00
【问题描述】:

我有一个 VB.NET 2010 应用程序,它将数据从 SQL Server 加载到 datagridviewadapter.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


【解决方案1】:

您当前进行过滤的方式是创建一个新的 DataTable 并将其绑定到您的 DataGridView 数据源。这会破坏原始 DataSet 和该表之间的关联,因此当您调用 Update 时,您的更改不会恢复。

尝试更改为类似下面的代码(我省略了 try catch 块,它们不会改变解决您问题的想法)。

当您第一次将数据源设置为网格时,请将其设置为表单级别的私有 dvSection 字段:

Public Class Form1

    ' Here we declare some class level variables. 
    'These are visible to all members of the class

    Dim dvSection As DataView
    Dim tableAdapter As New DataSet1TableAdapters.CustomersTableAdapter()
    Dim ds As New DataSet1()


    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

       ' We fill our dataset - in this case using a table adapter
        tableAdapter.Fill(ds.Customers)
        dvSection = ds.Customers.DefaultView

        DataGridView1.DataSource = dvSection

    End Sub

    ' Here is the code to filter.
    ' Note how I refer to the class level variable dvSection
    Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        Dim filter As String 

        lblDepartmentId.Text =   ds.Tables("department").Rows(cmbDepartment.SelectedIndex)(0) 

        filter = "dprtId = " & lblDepartmentId.Text 
        dvSection.RowFilter = filter 

        dvSection.RowFilter = filter

    End Sub

    ' And here is the update code referring to the class level table adapter
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        tableAdapter.Update(ds.Customers)
    End Sub
End Class

另一种可能使事情变得更容易的方法是引入绑定源并在其上设置过滤器。但是,两者都应该可以正常工作。

【讨论】:

  • 非常感谢 David 先生的协助,它运行良好,但出现了另一个问题。当我从组合框中选择时,过滤不起作用。任何的想法?对不起,也许我打扰你了
  • @user1548974 只要您在引用 dvSection 时引用的是同一个对象,并且只要该对象是 DataGridView 的数据源,那么对 dvSection 应用行过滤器应该可以工作。您是否将 dvSection 设为类(表单)级别的字段?
  • 尊敬的大卫先生,再次感谢您。我这样解决了这个问题: Private Sub cmbDepartment_SelectedIndexChanged(...) Handles cmbDepartment.SelectedIndexChanged Dim filter As String dvSection = ds.Tables("section").DefaultView; dataGridView1.DataSource = dvSection;尝试 lblDepartmentId.Text = ds.Tables("department").Rows(cmbDepartment.SelectedIndex)(0) filter = "dprtId = " & lblDepartmentId.Text dvSection.RowFilter = filter Catch ex As Exception MsgBox(Err.Description) End Try结束子
  • @user1548974 很高兴你有这个工作 - 看起来你的代码和我的一样。如果是,则将我的答案标记为已接受,以便人们知道它有效。如果不是,请将代码发布为您自己的答案。 StackOverflow 上的评论不是代码的地方,它们很难阅读,并且在人们寻求帮助时不会很突出。
  • @user1548974 啊 - 刚刚发现差异(cmets 中的代码很难阅读)使用默认视图的本地副本并在那里应用过滤器在功能上与我将其放置在类级别字段。他们应该工作相同。您是否清楚 VB.net 代码中的各种范围?正如我所说,VB.Net 不是我的主要语言,所以你的语言需要工作可能是有原因的。当我有时间时,我会在我的电脑上模拟两者。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-04
  • 2017-06-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多