数据网格视图更新、编辑和删除在 vb.net windows 窗体中使用多个表填充数据网格视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20944962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:21:28  来源:igfitidea点击:

data grid view update ,Edit and delete in vb.net windows form that using multiple table to populate datagridview

vb.netwinformsdatagridview

提问by user3106114

I am new to windows Forms applications...

我是 Windows 窗体应用程序的新手...

I am working in VB.NET windows forms and dealing with DataGridView...

我在 VB.NET windows 窗体中工作并处理 DataGridView ...

I have populated that DataGridView from two Table

我已经从两个表中填充了 DataGridView

Please tell me How to Add , Delete and Edit/Update the records of the DATAGridview back to the DataBase.

请告诉我如何将 DATAGridview 的记录添加、删除和编辑/更新回数据库。

I am using SQLSERVER 2008 DataBase

我正在使用 SQLSERVER 2008 数据库

I have two tables 1->CompanyMaster_tblin this having Two fields . Cid and CompanyName,

我有两个表1->CompanyMaster_tbl,其中有两个字段。Cid and CompanyName,

Cid is the primary key of this table

Cid是这张表的主键

2->DepartmentMaster_tblin this having 4 fields. dtid,dtname,dtphon,dtmail,Cid.

2->DepartmentMaster_tbl在这有 4 个字段。dtid,dtname,dtphon,dtmail,Cid.

dtid is the primary key,and Cid is the foreign key.. My data gridview look like this:enter image description here

dtid 是主键,Cid 是外键.. 我的数据 gridview 看起来像这样:在此处输入图片说明

Dim adapter As SqlDataAdapter
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

  'NOTE: I removed the Using statements to ease your tests/understanding, but you shouldn't let the connections opened (or, at least, set connection-release part somewhere)
  Dim con As SqlConnection = New SqlConnection() 'SET THE CONNECTION STRING
  con.Open()

  adapter = New SqlDataAdapter("select c.CompanyName,d.dtName,d.dtPhone,d.dtEmail  from CompanyMaster_tbl c join  DepartmentMaster_tbl d on c.Cid=d.cId", con)

  Dim dt As DataTable = New DataTable()
  adapter.Fill(dt) 'Filling dt with the information from the DB
  gv.DataSource = dt 'Populating gv with the values in dt

End Sub

in update button i wrote code like this:

在更新按钮中,我写了这样的代码:

Dim dt1 As DataTable = DirectCast(gv.DataSource, DataTable)
adapter.Update(dt1)

but after editing anything in gridview,,i click the update button,,but i am getting error in this row

但是在 gridview 中编辑任何内容后,我单击了更新按钮,但在这一行中出现错误

da.Update(dt1)

da.Update(dt1)

Error: Update requires a valid UpdateCommand when passed DataRow collection with modified rows.

错误:当传递带有修改行的 DataRow 集合时,更新需要有效的 UpdateCommand。

Thanks in advance

提前致谢

采纳答案by varocarbas

If you want to keep some synchronisation between the DataGridViewand the DB, you shouldn't add columns/rows manually, but rely on the DataSourceproperty. Sample code adapted to your case:

如果要在DataGridView和 DB之间保持一些同步,则不应手动添加列/行,而应依赖于该DataSource属性。适用于您的案例的示例代码:

Using con As SqlConnection = New SqlConnection() 'You have to set the CONNECTION STRING
    con.Open()

    Using adapter As SqlDataAdapter = New SqlDataAdapter("select c.CompanyName,d.dtName,d.dtPhone,d.dtEmail  from CompanyMaster_tbl c join  DepartmentMaster_tbl d on c.Cid=d.cId", con)

        Dim dt As DataTable = New DataTable()
        adapter.Fill(dt) 'Filling dt with the information from the DB
        gv.DataSource = dt 'Populating gv with the values in dt

    End Using
End Using

The code above extracts all the information you want from the DB, puts it into a DataTable(dt) and then feeds it to the DataGridViewas DataSource. gvhas now all the values from dt; also any change in dtis reflected in gvand vice versa (this coupling is almost perfect, at least, when updating values; there might be some problems while deleting rows/columns or changing their basic configuration). You might even keep adapteras a global variable (outside the Usingstatement) and rely on it to update the DB regularly (via adapter.Update(dt), for example).

上面的代码从数据库中提取您想要的所有信息,将其放入DataTable( dt) 中,然后将其提供给DataGridViewas DataSourcegv现在拥有来自的所有值dt;任何变化dt也反映在gv,反之亦然(这种耦合几乎是完美的,至少在更新值时;删除行/列或更改其基本配置时可能会出现一些问题)。您甚至可以保留adapter作为全局变量(在Using语句之外)并依靠它来定期更新数据库(adapter.Update(dt)例如,通过)。

Quite a few alternatives; but, in any case, relying on a DataSourceis certainly better under your conditions.

相当多的选择;但是,无论如何,DataSource在您的条件下,依靠 a肯定会更好。