如何将DataGridView中所做的所有更改保存到Access数据库 [英] How to save ALL changes made in DataGridView to Access database

查看:355
本文介绍了如何将DataGridView中所做的所有更改保存到Access数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想通过单击按钮来保存在datagridview中所做的所有更改,我使用了Access 2007,我使用了下面的代码,但错误显示更新需要有效的UpdateCommand,当传递带有修改行的Datarow集合时



  Dim  i < span class =code-keyword>作为 整数 
尝试
i = da.Update(ds, WorkRecords
MsgBox( 记录已更新=& i)
Catch ex As 异常
MsgBox(ex.Message)
结束 < span class =code-keyword>尝试





任何帮助将不胜感激。

< b>谢谢!

解决方案

这是经常被问到的问题。请参阅此处 [ ^ ]。


试试这个代码对我有用。

 Dim i As Integer'变量i给出更新的记录数
'创建表
Dim dt As New DataTable
'创建数据适配器
Dim da As New OleDbDataAdapter
试试
'检索并填充datagridview
'************************************* **
'你的陈述
'************************************ ***

'填充数据表
da.Fill(dt)
'DataBindings
Your_DataGridView.DataSource = dt
i = da.Update(ds,WorkRecords)
MsgBox(已成功更改,&我和我记录已更新!,MsgBoxStyle.OkOnly,成功)

Catch ex As Exception
MsgBox(ex.Message)
结束尝试


Hi i want to save all changes made in datagridview by clicking a button,i used access 2007, i used the code below but error display "Update Requires a valid UpdateCommand when passed Datarow collection with modified Rows"

Dim i As Integer
        Try
            i = da.Update(ds, "WorkRecords")
            MsgBox("Records Updated= " & i)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try



Any Help will be appreciated.
Thanks!

解决方案

It's quite often asked question. Please see here[^].


Try this code it works for me.

Dim i As Integer ' The variable i gives the number of records updated
       'create a table
       Dim dt As New DataTable
       ' create a data adapter
       Dim da As New OleDbDataAdapter
       Try
           'retrieving and populating datagridview
           '***************************************
           ' your statements
           '***************************************

           'Populates the datatable
           da.Fill(dt)
           'DataBindings
           Your_DataGridView.DataSource = dt
           i = da.Update(ds, "WorkRecords")
           MsgBox("Changes made successfully, " & i & " records updated!", MsgBoxStyle.OkOnly, "Success")

       Catch ex As Exception
           MsgBox(ex.Message)
       End Try


这篇关于如何将DataGridView中所做的所有更改保存到Access数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆