在GRID VIEW中显示两个数据表 [英] displaying two data tables in GRID VIEW

查看:110
本文介绍了在GRID VIEW中显示两个数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个数据表,我将它们添加到数据集中;

代码

I have 2 data tables which i add to a data set;

code

DataSource drugInfo= new DataSource();
 DataTable productTable = new DataTable("Product_Name");
            DataColumn productCol = new DataColumn("Product");
            productTable.Columns.Add(productCol);
            DataRow productRow = productTable.NewRow();
            productRow[productCol] = product.PrimaryPreferredName;
            productTable.Rows.Add(productRow);
            drugInfo.Tables.Add(productTable);
            //End of product info           
            
            //INGREDIENTS INFO
            DataTable ingrdnts = new DataTable("Ingredients");
            DataColumn drugIngrdnts = new DataColumn("INGREDIENTS");
            ingrdnts.Columns.Add(drugIngrdnts);
            Ingredient[] drugIngrdnt = product.GetIngredients();
            foreach (Ingredient ingrdnt in drugIngrdnt)
            {
                DataRow ingName = ingrdnts.NewRow();
                ingName[i] = ingrdnt.Text;
                ingrdnts.Rows.Add(ingName);
                i++;
            }
            drugInfo.Tables.Add(ingrdnts);



我已将2个数据表添加到数据集中,并将其作为对Web服务的响应发送.

在主要形式中,我试图将其绑定到网格视图.



I have added 2 data tables to a data set and have sent it as a response to webservice.

In the main form i am trying to bind it to grid view.

GridView2.DataSource= drugInfo.Tables[0].DefaultView;


但是无法同时显示两个数据表..

源代码中有什么问题



如何在gridview中显示两个表....


But am not able to display both the data tables..

what''s the problem in the source code

or

How to display both the table in gridview....

推荐答案

但是无法同时显示两个数据表.
您一次只能将一个表绑定到gridview.尽管您的数据集可能包含多个表,但是在运行时,只能有一个表作为gridview的源.

您最多只能通过DataRelation连接两个表并相应地使用它.如果没有关系,则整理两个表的数据的最佳方法是在查询本身中将它们一起提取.
But am not able to display both the data tables..
You can bind only one table at one time to the gridview. Though your dataset might contain multiple tables but at runtime you can have only one table as a source to the gridview.

maximum you can do is connect two tables via DataRelation and use it accordingly. If there is no relation then the best way to collate data of two table would be to fetch them together in the query itself.


在查询中使用联接,该联接仅返回一个表... ...
Use join in the query which return only one table......


如果您的产品和成分具有父子关系,则可以将其显示为嵌套网格视图或具有两个网格视图,其中第二个网格视图获取数据取决于来自第一个网格视图.如果您加入一个父子表进行显示,则将形成乘法表.例如,父表中有2行,而每个子表中有3行与子表相关,那么您将有6行要显示.您是否认为它比在第一个gridview中单击某些父行时将其单独显示在gridview中好用?

当您进行非规范化和显示时,您将再次在编辑/更新时遇到并发问题.否则,您将搜索哪些数据属于父级,哪些数据属于子级以更新父级,然后更新子级.

如果两个表都具有一对一关系,则最好进行联接.在其他情况下,如果孩子是已定义的列表,则可以使用基于列表的控件来显示,以便于编辑.

祝你好运
If your Product and Ingredients have parent child relationship then you may display it as nested grid view or have two grid views where the second grid view fetch data depends on the command from the first grid view. If you join a parent child tables for display you will be forming multiplication tables. For example 2 rows in parent table and each related to 3 rows in child table you will be having 6 rows to display. Do you think it is user friendly than display it separately in a gridview when you click some parent row in the first gridview?

When you denormalize and display you will be again held up with concurrency issues on editing/ updating. Or else you will be searching around which data belongs to parent and which data belongs to child to update parent and then child.

If both table has one to one relationship it is good to go with a join. In other cases if the child is a defined list you may use list based controls to display which facilitate editing.

Good luck


这篇关于在GRID VIEW中显示两个数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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