带外键的dataGridView [英] dataGridView with Foreign KEY

查看:100
本文介绍了带外键的dataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我是一个带有两个表的简单数据集 地区和国家。

I a simple Dataset with two tables  Region and Nations.

对于每个地区,都有一个与国家主键相关的外键表

For each Region there is a Foreign KEY related to Primary Key of Nations Table

然后我有一个带有 的主表格。 DGV显示所有具有区域名称的区域,国家ID国家描述

Then I have a Main Form with a  DGV to show all Regions with Region Name, Nation ID Nation Description

Dim QueryS As String = <sql> SELECT [RegioneID] ,[RegioneDes] ,[RegioneNazioneID] ,[NazioneDes] FROM Tabel.TabRegioni INNER JOIN Tabel.TabNazioni ON Tabel.TabRegioni.RegioneNazioneID = Tabel.TabNazioni.NazioneID

ORDER BY [RegioneID] ASC

ORDER BY [RegioneID] ASC

< / sql> .Value


尝试
使用CONN作为新的SqlConnection {.ConnectionString = VG.FullCS}
使用CMD = New SqlCommand()使用{.Connection = CONN,
。 CommandType = CommandType.Text}

使用DAL_DA作为新的SqlDataAdapter使用
{.SelectCommand =使用{.Connection = CONN}的新SqlCommand}
CONN.Open()
DAL_DA.SelectCommand.CommandText = QueryS
DAL_DA.Fill(DAL_DS," TabRegioni")
返回DAL_DS
结束使用
结束使用
结束使用

</sql>.Value Try Using CONN As New SqlConnection With {.ConnectionString = VG.FullCS} Using CMD = New SqlCommand() With {.Connection = CONN, .CommandType = CommandType.Text} Using DAL_DA As New SqlDataAdapter With {.SelectCommand = New SqlCommand With {.Connection = CONN}} CONN.Open() DAL_DA.SelectCommand.CommandText = QueryS DAL_DA.Fill(DAL_DS, "TabRegioni") Return DAL_DS End Using End Using End Using

DGV b绑定到TabRegioni的BindingSource

The DGV bound to a BindingSource bound to TabRegioni

这很好用,但是当我尝试用另一个表单修改一行,或者在TabRegioni中插入一个新行时,

This works well, but when I try to modify a row with another form, or insert a new row in TabRegioni,

操作顺利(在TabRegioni和数据库中)但DGV中的Nation描述没有更新。

the operation goes well (both in TabRegioni and Database) but the Nation Description in the DGV is not update.

我的错误是什么?

推荐答案

也许是因为你在另一种形式中使用不同的数据集,因此,如果您希望您的更新也以第一种形式出现,那么请将数据集设为全局。
Maybe it because you use a different dataset in another form, so if you want your update also seen in first form then make dataset as global.


这篇关于带外键的dataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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