Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,

I have 5 gridviews in a page. 4 of these have separate queries and datasets say ds1, ds2, ds3, ds4.

Now left one gridview is populated from combined data of selected columns of other datasets, like:

ds1col2, ds2col2, ds3col2, ds4col2, ds1col3+ds2col3+ds3col3+ds4col3

All the gridviews have bound-field datafields.

Is this combination possible?

Thank you..!!

What I have tried:

I have tried merging, but that combines all columns from all tables and that works only on 2 tables at a time.
Posted
Updated 31-Oct-17 1:26am
v2
Comments
Karthik_Mahalingam 31-Oct-17 6:23am    
how will you combine ? do you have common id across the datasets ?
planetz 31-Oct-17 6:55am    
yes, there is a column that is same for all datasets, but that is not something like a primary key. Just like a student list say, where student name is the common field, and their individual subject marks are 4 datasets and then there is a summary that contains individual subject marks and also total marks.
Kornfeld Eliyahu Peter 31-Oct-17 6:25am    
You should do it at the database level... Coding options are very complicated...
planetz 31-Oct-17 6:58am    
the output can be achieved by query...but it will be writing same selected lines from 4 different queries. But if thie procedure I asked is even more complicated and time taking, then may be query is a better option.

1 solution

refer this and customize for your need

static void Main(string[] args)
       {
           //ds1col2, ds2col2, ds3col2, ds4col2, ds1col3+ds2col3+ds3col3+ds4col3

           DataTable ds1 = new DataTable();
           ds1.Columns.Add("CommonCol");
           ds1.Columns.Add("Col2");
           ds1.Columns.Add("Col3");
           ds1.Rows.Add("A", 1, 2);
           ds1.Rows.Add("B", 1, 2);

           DataTable ds2 = new DataTable();
           ds2.Columns.Add("CommonCol");
           ds2.Columns.Add("Col2");
           ds2.Columns.Add("Col3");
           ds2.Rows.Add("A", 1, 2);

           DataTable ds3 = new DataTable();
           ds3.Columns.Add("CommonCol");
           ds3.Columns.Add("Col2");
           ds3.Columns.Add("Col3");
           ds3.Rows.Add("A", 1, 2);

           DataTable ds4 = new DataTable();
           ds4.Columns.Add("CommonCol");
           ds4.Columns.Add("Col2");
           ds4.Columns.Add("Col3");
           ds4.Rows.Add("A", 1, 2);

           DataTable ds5 = new DataTable();
           ds5.Columns.Add("CommonCol");
           ds5.Columns.Add("Col1");
           ds5.Columns.Add("Col2");
           ds5.Columns.Add("Col3");
           ds5.Columns.Add("Col4");
           ds5.Columns.Add("Col5");


           foreach (DataRow row in ds1.Rows)
           {
               string common = row["CommonCol"].ToString();
               double ds1col2 = GetValue(common, ds1);
               double ds2col2 = GetValue(common, ds2);
               double ds3col2 = GetValue(common, ds3);
               double ds4col2 = GetValue(common, ds4);
               double total = ds1col2 + ds2col2 + ds3col2+ ds4col2;
              var newRow =  ds5.NewRow();
              newRow["CommonCol"] = common;
              newRow["Col1"] = ds1col2;
              newRow["Col2"] = ds2col2;
              newRow["Col3"] = ds3col2;
              newRow["Col4"] = ds4col2;
              newRow["Col5"] = total;
              ds5.Rows.Add(newRow);

           }

       }

     static  double GetValue(string common, DataTable dt) {
           double returnValue = 0;
           var rows = dt.Select("CommonCol = '" + common + "'");
           if (rows.Length > 0)
               double.TryParse(rows[0]["Col3"].ToString(), out returnValue);
          return returnValue;

       }
 
Share this answer
 

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