DataGridView更新数据源
DataGridView后期绑定数据库,希望在数据改变后,使用按钮命令更新数据源,在该按钮Click事件里添加了下面的代码,但是更新无效,请问是哪的问题,应如何处理?
Dim dt As DataTable = DataGrd.DataSource
Dim Adapter As New OleDb.OleDbDataAdapter
Try
If dt.GetChanges() IsNot Nothing Then
Adapter.Update(dt)
End If
Catch ex As Exception
Adapter = Nothing
dt = Nothing
End Try
Adapter = Nothing
dt = Nothing
[解决办法]
给一个例子看看是否有帮助:
使用数据适配器构建器Builder
当手动写数据适配器dataAdpter时用:
Dim conn As New SqlConnection("Server=(local);uid=sa;pwd=;database=xtreme")
Dim connStr As String = "Server=(local);uid=sa;pwd=;database=xtreme"
Dim myDataset as new dataset
dim cmd As New SqlCommand
Dim sqlda as sqldataAdapter = New SqlDataAdapter("SELECT * FROM 客户", connStr)
Dim Builder As SqlCommandBuilder = New SqlCommandBuilder(sqlda)
‘有了上面一句后,
Sqlda.fill(mydataset,”客户
’就可用更新删除添加了。
Sqlda.update(mydataset.table(0))
‘查看构建器内所建的命令。
Debug.write(Builder.GetUpdateCommand.CommandText)
‘得到所有row.RowState为Change的记录。
myDataSet1 = myDataSet.GetChanges