我正在尝试将对datagridview所做的更改保存到表tbl_invent中,我所做的更改提交到datagridview但它没有保存到表(数据库),也没有任何错误,我收到的所有内容都是一条消息说“记录已更新= 0”。有谁能指出我正确的方向?
Dim da As New SQLiteDataAdapter("select * from tbl_Invent", connection)
Dim ds As New DataSet
'Dim cmdbuilder As New SQLite.SQLiteCommandBuilder(da)
Dim i As Integer
da.TableMappings.Add("tbl_Invent", "tbl_Invent") 'add due to error unable to Update unable to find TableMapping['Table'] or DataTable 'Table'
Try
i = da.Update(ds, "tbl_Invent")
MsgBox("Records Updated= " & i)
Catch ex As Exception
MsgBox(ex.Message)
End Try
connection.Close()
我已经查看了这个主题: How to save changes from DataGridView to the database?
- 和 -
Datagridview save changes to Database vb.net
非常感谢你。
答案 0 :(得分:0)
如果您在创建/填充DataTable
和尝试保存更改之间未进行任何更改,则显然您不会保存任何更改。您需要创建DataTable
,填充它并将其绑定在一个方法(可能是表单的Load
事件处理程序)中,然后用户进行更改,然后保存来自同一{的更改{ {1}}在另一种方法中(可能是DataTable
的{{1}}事件处理程序。例如
Click
表单创建Button
时,用户在网格中进行更改并点击Private table As New DataTable
Private adapter As New SqlDataAdapter("SQL query here", "connection string here")
Private builder As New SqlCommandBuilder(adapter)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
adapter.Fill(table)
BindingSource1.DataSource = table
DataGridView1.DataSource = BindingSource1
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
BindingSource1.EndEdit()
adapter.Update(table)
End Sub
然后您保存来自同一DataTable
的更改,而不是新的您刚创建的一个不包含任何更改的内容。