数据网格视图更新,以vb.net Windows窗体编辑和删除,使用多个表填充datagridview [英] data grid view update ,Edit and delete in vb.net windows form that using multiple table to populate datagridview
问题描述
我是Windows窗体应用程序的新手...
I am new to windows Forms applications...
我正在VB.NET Windows窗体中工作并处理DataGridView ...
I am working in VB.NET windows forms and dealing with DataGridView...
我已经从两个表中填充了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.
我正在使用SQLSERVER 2008数据库
I am using SQLSERVER 2008 DataBase
我有两个表 1-> CompanyMaster_tbl
在此具有两个字段。 Cid and CompanyName
,
I have two tables 1->CompanyMaster_tbl
in this having Two fields . Cid and CompanyName
,
Cid是此表的主键
Cid is the primary key of this table
2-> DepartmentMaster_tbl
,其中有4个字段。 dtid,dtname,dtphon,dtmail,Cid
。
2->DepartmentMaster_tbl
in this having 4 fields. dtid,dtname,dtphon,dtmail,Cid
.
dtid是主键,Cid是外键。.
我的数据gridview看起来像这样:
dtid is the primary key,and Cid is the foreign key.. My data gridview look like this:
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
中的值填充gv像这样写代码:
in update button i wrote code like this:
Dim dt1 As DataTable = DirectCast(gv.DataSource, DataTable)
adapter.Update(dt1)
但是在gridview中编辑了所有内容之后,我单击了更新按钮,但是我正在此行中的错误
but after editing anything in gridview,,i click the update button,,but i am getting error in this row
da.Update(dt1)
da.Update(dt1)
错误:
通过带有修改后的行的DataRow集合时,更新需要有效的UpdateCommand。
Error: Update requires a valid UpdateCommand when passed DataRow collection with modified rows.
预先感谢
推荐答案
如果要在 DataGridView
和数据库之间保持某种同步,则不应添加列/ rows手动,但依赖于 DataSource
属性。适合您的情况的示例代码:
If you want to keep some synchronisation between the DataGridView
and the DB, you shouldn't add columns/rows manually, but rely on the DataSource
property. Sample code adapted to your case:
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
上面的代码从数据库中提取您想要的所有信息,并将其放入 DataTable
( dt
),然后将其作为 DataSource
馈送到 DataGridView
。 gv
现在具有 dt
中的所有值; dt
中的任何变化也反映在 gv
中,反之亦然(至少在更新时,这种耦合几乎是完美的值;删除行/列或更改其基本配置时可能会出现一些问题。您甚至可以将 adapter
保留为全局变量(在 Using
语句之外)并依靠它定期更新数据库。 (例如,通过 adapter.Update(dt)
)。
The code above extracts all the information you want from the DB, puts it into a DataTable
(dt
) and then feeds it to the DataGridView
as DataSource
. gv
has now all the values from dt
; also any change in dt
is reflected in gv
and 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 adapter
as a global variable (outside the Using
statement) and rely on it to update the DB regularly (via adapter.Update(dt)
, for example).
很多其他选择;但是无论如何,在您的情况下,依靠 DataSource
当然会更好。
Quite a few alternatives; but, in any case, relying on a DataSource
is certainly better under your conditions.
这篇关于数据网格视图更新,以vb.net Windows窗体编辑和删除,使用多个表填充datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!