Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
sir, i have 2 datatable that result show like:-

Datatable1:-
Studentid --  marks
  101      --  6
  102      --  4
  103      --  5


Datatable2:-
Studentid  --- marks
  101       --  6
  102       --  7
  103       --  5


now i need i third datatable that show the addition of this two datatables. and look like :-

Datatable3:-
Studentid  -- Marks
 101       -- 12
 102      --  11
 103     --   10


how can i do it?
Posted
Updated 1-Sep-14 5:36am
v2

Try this!

C#
int length = 10 //considering 10 rows
for(int i = 0 ;i < length ; i++)
{
    DataRow drow = dt.NewRow();
    for(int k = 0;k < 2;k++) //Considering 2 columns
    {
         if(k % 2 == 0)
         {
              drow[k] = dt1.Rows[i][k].ToString(); // your student id from 1st table
         }
         else
         {
              drow[k] = (Convert.ToInt32(dt1.Rows[i][k]) + Convert.ToInt32(dt2.Rows[i][k])).ToString();   //dt2 your second table
         }
    }  
     dt.Rows.Add(drow);

}
 
Share this answer
 
Comments
TCS54321 1-Sep-14 9:30am    
i need the code in vb.net
ChintanShukla 1-Sep-14 9:34am    
I gave you the logic. Convert code to VB.net by yourself
You can use LINQ, Its a C# code you can find the corresponding Vb code here

C#
var query =
    from table1 in Datatable1.AsEnumerable()
    join table2 in Datatable2.AsEnumerable()
    on Datatable1.Field <int>("Studentid") equals
    Datatable2.Field <int>("Studentid")
    select new
    {
        Studentid=
            Datatable1.Field <int>("Studentid"),
        Marks =
            Datatable1.Field <int>("marks") + Datatable2.Field <int> ("marks")
    };
 
Share this answer
 
v2
Comments
TCS54321 2-Sep-14 9:23am    
thanku mam..
Is this what you are looking for : Merging two datatable in memory and grouping them to get sum of columns[^]

Note : Convert the code to vb.net

Regards..
 
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