简体   繁体   English

使用 Vb.net 中的单个更新按钮在 DataGridView 中插入、删除和编辑数据

[英]Insert, Delete, and Edit Data in DataGridView using single Update Button in Vb.net

I am trying to have a add, delete, and edit function in Vb.net by using one update button and changing the values in the datagridview manually then hitting update.我试图在 Vb.net 中添加、删除和编辑 function,方法是使用一个更新按钮并手动更改 datagridview 中的值,然后点击更新。 However, I get an error stating "System.InvalidOperationException: 'Update requires a valid UpdateCommand when passed DataRow collection with modified"但是,我收到一条错误消息,指出“System.InvalidOperationException: 'Update requires a valid UpdateCommand when passed DataRow collection with modified”

Any Ideas?有任何想法吗? The error comes next to the da.Update(changes)错误出现在 da.Update(changes) 旁边

Also in the above code not shown in my code I have load_data() in the private form1_load sub.同样在我的代码中未显示的上述代码中,我在私有 form1_load 子中有 load_data() 。

Here is code:这是代码:

Dim da As New SqlDataAdapter
Dim dt As New DataSet

Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
    Dim cmd As New SqlCommandBuilder
    Dim changes As New DataSet
    changes = dt.GetChanges
    If changes IsNot Nothing Then
        da.Update(changes)
        da.Fill(dt)
        DataGridView1.DataSource = dt.Tables(0)
    End If
End Sub

Private Sub load_data()
    Using connection = New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=vbconnectionfinal;Integrated Security=True")

        da = New SqlDataAdapter("Select * From TrueTrack", connection)
        dt.Clear()
        da.Fill(dt)
        DataGridView1.DataSource = dt.Tables(0)

    End Using
End Sub

You are creating a SqlCommandBuilder but not associating it with a SqlDataAdapter , so what's the point?您正在创建SqlCommandBuilder但没有将其与SqlDataAdapter关联,那么有什么意义呢? The whole purpose of a command builder is to automatically generate action commands when you call Update on a data adapter.命令构建器的全部目的是在您在数据适配器上调用Update时自动生成操作命令。 When you create your data adapter, create the command builder immediately after and associate the two, which you would do by passing the data adapter as an argument to the constructor of the command builder.当您创建数据适配器时,请立即创建命令构建器并将两者关联起来,您可以通过将数据适配器作为参数传递给命令构建器的构造函数来实现。

The following code works for me.以下代码适用于我。

Dim da As New SqlDataAdapter
Dim dt As New DataSet
Dim connection = New SqlConnection("connection string")
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim cmd As New SqlCommandBuilder(da)
    Dim changes As New DataSet
    changes = dt.GetChanges
    If changes IsNot Nothing Then
        da.Update(changes)
        DataGridView1.DataSource = dt.Tables(0)
    End If
End Sub
Private Sub load_data()
    Dim cmd As SqlCommand = New SqlCommand("Select * From TrueTrack", connection)
    da = New SqlDataAdapter(cmd)
    dt.Clear()
    da.Fill(dt)
    DataGridView1.DataSource = dt.Tables(0)
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    load_data()
End Sub

Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
    connection.Close()
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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