数据网格更新 [英] data grid update
问题描述
以下代码:
尝试
da.Update(ds.Tables(dg1Name))
Catch ex As Exception
Console.WriteLine(ex.ToString)
MessageBox.Show(ex.ToString)
结束尝试
导致此错误:
System.InvalidOperationException:当
传递带有修改行的DataRow集合时,Update需要有效的UpdateCommand。
在System.Data.Common.DbDataAdapter.Update(DataRow [] dataRows,
DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataTable dataTable)<评论中的
.Borm1.Button1_Click(对象发件人,EventArgs e)
G:\ Shared \ VBShare\dotnet \Kevin \ comments \Form1。 vb:l ine 161
代码是名为Update的表单上的一个按钮。如果我在网格中更改数据
,则在按下更新按钮时会出现错误。任何建议都会非常感激。
凯文
The following code:
Try
da.Update(ds.Tables(dg1Name))
Catch ex As Exception
Console.WriteLine(ex.ToString)
MessageBox.Show(ex.ToString)
End Try
causes this error:
System.InvalidOperationException: Update requires a valid UpdateCommand when
passed DataRow collection with modified rows.
at System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows,
DataTableMapping tableMapping)
at System.Data.Common.DbDataAdapter.Update(DataTable dataTable)
at comments.Form1.Button1_Click(Object sender, EventArgs e) in
G:\Shared\VBShare\dotnet\Kevin\comments\Form1.vb:l ine 161
The code is behiend a button on the form called Update. If I change the data
in the grid I get the error when I press the update button. Any advice will
be much appreciated.
Kevin
推荐答案
Kevin,
你是如何创建更新命令的?
据我所知3种可能性
- 手工制作
- 使用AdapterWizard
- 使用命令构建器。
现在您的错误可能是一切。
Cor
Kevin,
How did you create your update command?
You have as far as I know 3 possibilities
- by hand yourself
- using an AdapterWizard
- using an commandbuilder.
Now your error can be everything.
Cor
Cor,
我没有创建更新命令,我用过以下代码填写
网格,更改了其中一条记录并认为数据集和数据
适配器负责其余部分?
>
Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As
System.EventArgs)处理MyBase.Load
Dim命令As String = Microsoft。 VisualBasic.Command
Dim strDatabase,strSQL As String
strDatabase = Mid(命令,1,7)
gstrCustomer = Mid(命令,9,12)
gstrOrder = Mid(命令,22,8)
gstrOrdType = Mid(命令,31,1)
调用connectSQL(strDatabase)
''填充网格
strSQL =" select''" &安培; " Y" &安培; "''as Selected,seqNo,comment from
sql_oe_comments where custNo =''" &安培; gstrCustomer& "''"
cb =新的SqlCommandBuilder(da)
ds =新数据集()
da =新的SqlDataAdapter(strSQL,sqlConn)
da.Fill(ds,dg1Name)
使用DG1''这是数据网格在表格上
.DataSource = ds.Tables(dg1Name)
.AllowSorting = True
.AlternatingBackColor = System.Drawing.Color.Bisque
结束
''格式化网格
调用formatGridDG1()
结束Sub
" Cor Ligthert"写道:
Cor,
I did not create an update command, I used the following code to fill the
grid, changed one of the records and thought that the dataset and data
adaptor took care of the rest?
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim commands As String = Microsoft.VisualBasic.Command
Dim strDatabase, strSQL As String
strDatabase = Mid(commands, 1, 7)
gstrCustomer = Mid(commands, 9, 12)
gstrOrder = Mid(commands, 22, 8)
gstrOrdType = Mid(commands, 31, 1)
Call connectSQL(strDatabase)
''fill the grid
strSQL = "select ''" & "Y" & "'' as Selected, seqNo, comment from
sql_oe_comments where custNo = ''" & gstrCustomer & "''"
cb = New SqlCommandBuilder(da)
ds = New DataSet()
da = New SqlDataAdapter(strSQL, sqlConn)
da.Fill(ds, dg1Name)
With DG1 ''this is the datagrid on the form
.DataSource = ds.Tables(dg1Name)
.AllowSorting = True
.AlternatingBackColor = System.Drawing.Color.Bisque
End With
''format the grid
Call formatGridDG1()
End Sub
"Cor Ligthert" wrote:
凯文,
你是如何创建更新命令的?
据我所知,有3种可能性
- 亲自动手
- 使用AdapterWizard
- 使用命令构建器。
现在你的错误就是一切。
Cor
Kevin,
How did you create your update command?
You have as far as I know 3 possibilities
- by hand yourself
- using an AdapterWizard
- using an commandbuilder.
Now your error can be everything.
Cor
Kevin,
您展示的代码中有一个命令构建器,我要求。
顺便问一下,你确定这个有效吗
Kevin,
There is a commandbuilder in the code you showed and that I asked for.
By the way, are you sure that this works
da.Fill(ds,dg1Name)
da.Fill(ds, dg1Name)
而不只是这个
da.Fill(ds)
我们假设您的表单上有一个按钮,您点击的内容和
点击事件你设置这段代码
BindingContext(ds.Tables(0))。EndCurrentEdit()
试试
da.Update(ds.Tables(0))
Catch ex As Exception
Console.WriteLine(ex.ToString)
梅萨geBox.Show(ex.ToString)
结束尝试
比不好的代码,但它可能会工作,当你是一个新手
是最重要的事情之一。
我希望这有帮助
Cor。
And not just this
da.Fill(ds)
Let us assume that you have a button on your form on what you click and in
that click event you set this code
BindingContext(ds.Tables(0)).EndCurrentEdit()
Try
da.Update(ds.Tables(0))
Catch ex As Exception
Console.WriteLine(ex.ToString)
MessageBox.Show(ex.ToString)
End Try
Than it is bad code, however it will probably work and when you are a newbie
is that one of the first things that is important.
I hope this helps
Cor.
这篇关于数据网格更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!