我如何从两个表中获取网格视图中的数据? [英] how do i get data in gridview from two table?

查看:102
本文介绍了我如何从两个表中获取网格视图中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个表,s1和s2

s1字段---- NO,NAME
s2字段----- NO,CITY,国家/地区
我不需要GRIDVIEW中的名称,城市,国家/地区

下面的代码不适用于gridview中2个表的数据

i have 2 table,s1 and s2

s1 field ----NO,NAME
s2 field-----NO,CITY,country
I NEED NO,NAME,CITY,country IN GRIDVIEW

below code does not work fatch data from 2 table in gridview

Private Sub binddata()
cn = New SqlConnection("Data Source=abc-64B13238\SQLEXPRESS;Initial Catalog=std;Integrated Security=True")
        adp = New SqlDataAdapter("select * from s1", cn)
        adp.Fill(ds, "no")
        adp = New SqlDataAdapter("select * from s2", cn)
        adp.Fill(ds1, "no")
        ds.Merge(ds1)
        DataGridView1.DataSource = ds
End sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Binddata()
End sub

推荐答案

不是与此相同 [ ^ ]你几天前问的?

顺便说一句,看起来您的两个表是完全独立的.
您为什么不尝试类似:
Isn''t this the same as this[^] that you asked few days back?

BTW, looks like your two tables are totally independent.
Why don''t you try something like :
SELECT * FROM s1, s2 


并将数据放在表中以供使用?

没有共同的专栏吗?如果是这样的话:


and put the data in a table to use it?

Is NO a common column? If so then:

SELECT s1.NO, s2.CITY, s2.COUNTRY, s1.NAME  FROM s1 INNER JOIN ON s1.NO = s2.NO



无需两个数据集,就可以在任何地方进行合并.单个查询返回您需要的数据.



No need to have two dataset and do a merge anywhere. Single query returns the data you need.


如果两个表都可以具有一对一的关系,请在其中一个表中创建一个外键字段.执行联接查询并使用结果绑定gridview.

否则,如何将表中的一行与另一表中的另一行进行匹配. Gridview然后需要获取人工智能
If Both tables can have one to one relationship, create a foreign key field in one of the table. Execute a join query and use the result to bind the gridview.

Other wise how you can match one row from a table to other row in other table. Gridview need to get artificial intelligence then




一种可能的建议:
1.您可以创建一个DataTable dt3来合并table1和table2.然后将dt3绑定到gridview.
2.更新数据后,将dt3与table1进行比较.将更改保存到table1.
3.将table1更新到数据库.
这可能是解决您问题的方法.
我希望这可以帮助您解决问题.


One possible suggestion:
1. You may create a DataTable dt3 to merge table1 and table2. Then bind dt3 to gridview.
2. After update data, compare your dt3 to table1. Save the changes to table1.
3. Update table1 to database.
This may be a way for your problem.
I hope this can help resolve your problem.


这篇关于我如何从两个表中获取网格视图中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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