Click here to Skip to main content
15,174,758 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 data tables which i add to a data set;

code
C#
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);


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;

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....
Posted
Updated 2-May-11 23:38pm
v2

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.
   
Comments
Albin Abel 3-May-11 5:57am
   
This is correct. When the two tables are not related why people even think of display it together. My 5
Sandeep Mewara 3-May-11 7:17am
   
Lets not ask OP. ;)
Use join in the query which return only one table......
   
Comments
Albin Abel 3-May-11 5:59am
   
How about intersect, how about union. Basically the tables needs a relationship. My 5, join is an option
Ra-one 3-May-11 6:26am
   
It can be used according to requirement. But if you want your application should perform well than avoid union and stay tuned with join......:)
Albin Abel 3-May-11 6:37am
   
lol the union function deprecated
Ra-one 3-May-11 7:37am
   
not really, but the situation in which table is denormalized than it is really helpful. But Its always better to have a normalized database accept some exceptional situation...
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
   
Comments
Sandeep Mewara 3-May-11 7:17am
   
Nicely exaplained! 5!
Ra-one 3-May-11 7:28am
   
My 5 for the way you explained....
Hi ,
is possible to join tables.
if yes u can both tables & bind to Gridview.

if it like master details table. then u can refer following link.

http://mattberseth2.com/demo/Default.aspx?Name=4+New+Grouping+Grid+Skins%3a+Vista%2c+Bold%2c+Win2k3+and+Soft&Filter=All[^]
   
My suggestion is to make it into one table or marge it in to one.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900