删除链接表中的行 [英] Delete row in linked tables

查看:78
本文介绍了删除链接表中的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码删除包含两个彼此链接的表的行

但是下一个错误发生了


如果DataGridView1.RowCount< = 0则
退出Sub
结束如果
DataGridView1.ClearSelection()
Dim a As Integer
DataGridView1.Rows(DataGridView1.CurrentRow.Index).Selected = True
a = DataGridView1.Rows.GetLastRow(DataGridViewElementStates.Selected)。 ToString + 1

Application.DoEvents()

DataGridView1.Rows.RemoveAt(DataGridView1.CurrentRow.Index)
Validate()
DataAdapter1.Update (DataSet1.Tables(" tb1,tb2"))
DataSet1.AcceptChanges()

错误


多个基础不支持动态sql生成表。






解决方案

发生这种情况的原因是DataAdapter不够智能为DataAdapter创建更新语句,因为它不"知道"你的表关系。


解决这个问题的方法是为DataAdapter提供你自己的UPDATE语句。


如果有人问我,如何做到这一点,不是使用DataAdapter而是使用DataSet,使用每个DataTable的GetChanges方法,然后创建适当的连接和命令来更新。


I有一个演示,以演示如何在
此代码示例中使用GetChanges


在旁注中,任何时候使用使用DataSet或DataTable的DataSource设置DataGridView(和/或使用DataSet或Datatable设置的其他控件)始终执行CRUD操作,例如不通过DataGridViewRow集合
而是从基础DataTable中删除行。如果您正在使用的DataSet.Table使用BindingSource,例如SomeBindingSource.DataSource = SomeDataSet.Table(" Customers")然后DataGridView1.DataSource = SomeBindingSource你可以使用SomeBindingSource.RemoveCurrent
来移除当前行。 



Use the following code to delete a row with two tables linked to each other
But the next mistake occurs

   If DataGridView1.RowCount <= 0 Then
                Exit Sub
            End If
            DataGridView1.ClearSelection()
            Dim a As Integer
            DataGridView1.Rows(DataGridView1.CurrentRow.Index).Selected = True
            a = DataGridView1.Rows.GetLastRow(DataGridViewElementStates.Selected).ToString + 1

            Application.DoEvents()

            DataGridView1.Rows.RemoveAt(DataGridView1.CurrentRow.Index)
            Validate()
            DataAdapter1.Update(DataSet1.Tables("tb1,tb2"))
            DataSet1.AcceptChanges()

The error

dynamic sql generation is not supported against multiple base tables.


解决方案

The reason this is happening is that the DataAdapter is not smart enough to create the update statement for the DataAdapter as it does not "know" your table relations.

The way to get around this is to provide your own UPDATE statement for the DataAdapter.

If someone was to ask me, how to do this it would not be with a DataAdapter but using a DataSet, working with GetChanges method of each DataTable and then create appropriate connection and command(s) to to the updates.

I have a demo to show how to work with GetChanges in this code sample.

On a side note, any time you have a DataGridView (and/or other controls setup with a DataSet or Datatable) setup using the DataSource that uses a DataSet or DataTable always perform CRUD operations such as removal of a row not via the DataGridViewRow collection but from the underlying DataTable. If the DataSet.Table you are working with worked with a BindingSource e.g. SomeBindingSource.DataSource = SomeDataSet.Table("Customers") then DataGridView1.DataSource = SomeBindingSource you can use SomeBindingSource.RemoveCurrent to remove the current row for example. 


这篇关于删除链接表中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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