将两个表连接到网格视图 [英] connect two table to grid view

查看:74
本文介绍了将两个表连接到网格视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个表,tb1和tb2

如何在1个gridview中同时获取两个表数据?

tbl1字段---- NO,NAME

TBL2领域-----没有,城市,电话

我在GRIDVIEW中不需要城市电话



THNK U ADVV

i have 2 table,tb1 and tb2
how do i fatch both table data in 1 gridview?
tbl1 field ----NO,NAME
TBL2 field-----NO,CITY,PHONE
I NEED NO NAME CITY PHONE IN GRIDVIEW

THNK U ADVV

推荐答案

使用 sqljoins [ ^ ]从两个表中获取列并绑定gridview。 />


或查看此处 [ ^ ]
Use sqljoins[^] to get the columns from both tables and bind gridview.

or check here[^]


请参见此处 [ ^ ]。


您可以使用列别名来命名从数据库中提取的列。使用列别名,您可以将别名与DataGridView控件的列链接。



首先,您必须从数据库中获取包含列别名的数据并将其存储在DataTable中。您可以使用以下代码:



You can use column aliases to name the columns that you fetch from the database. Using column aliases, you can link the alias name with the column of the DataGridView control.

First, you'll have to fetch the data with column aliases from the database and store it in a DataTable. You can use the following code for that:

SqlConnection cn = new SqlConnection (...);

string sql = "Select p.no as 'Num', p.Name as 'Name', q.city as 'City', q.Phone as 'Phone' from tab1 p, tab2 q; where p.no = q.no";

SqlCommand sCmd = new SqlCommand(sql,cn);
SqlDataAdapter sAdap = new SqlDataAdapter(sCmd);
DataSet ds = new DataSet();

sAdap.Fill(ds);

DataTable dt = ds.Tables[0];





现在创建您的DataGridview。选择编辑列(在组件菜单中)并添加四列。您可以根据自己的意愿为列命名或提供标题。对于每个列的DataProperty值,分别为列提供值Num,Name,City和Phone。



现在将DataGridView的DataSource设置为Datatable 。 (请参阅下面的代码。)





Now create your DataGridview. Choose Edit Columns (in the Component menu) and add four columns. You can name the columns or provide headers based on your wish. For the DataProperty Value of each of the columns provide values as Num, Name, City and Phone for the columns respectively.

Now set the DataSource of the DataGridView to the Datatable. (Refer the code below).

this.DataGridView1.DataSource = dt;







应该这样做!



~干杯




That should do it!

~ Cheers


这篇关于将两个表连接到网格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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