Click here to Skip to main content
15,885,097 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I have a datatable

for eg :
Id | Name | Subject | Marks
1 siva Phy 60
1 siva Che 80

I want the result to be in a single row at new datatable

Id | Name | Subject | Marks | Subject | Marks
1 siva phy 60 che 80

how to do it in c# ?
Posted
Updated 10-Feb-14 1:35am
v2
Comments
sahabiswarup 10-Feb-14 7:54am    
try PIVOT function to get your result, or you may use 2 different datatable and then merge them in one final datatable.

Hi Murugappan

In DataTable you cannot have duplicate Columns.
What you can do is
Get distinct ID and Name from the table , for that each distinct row you filter the Subjects part into DataRow[] array and then load the array of data into the new table. as SUbject1,Marks1,Subject2,Marks2,Subject3,Marks3

I have done a sample workout for you..
Try to deubg and modify as per your need..enjoy :)
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Data;
using System.Collections.Concurrent;
class Program
{
    public static void Main(string[] args)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("ID", typeof(int));
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Subject", typeof(string));
        dt.Columns.Add("Marks", typeof(int));
        dt.Rows.Add(1, "siva", "phy", 80);
        dt.Rows.Add(1, "siva", "che", 60);
        dt.Rows.Add(1, "siva", "mat", 50);
        dt.Rows.Add(1, "siva", "geo", 40);

        // consider max of 4 subjects
        DataTable dtout = new DataTable();
        dtout = dt.Clone();
        dtout.Columns.Add("Subject1", typeof(string)); dtout.Columns.Add("Marks1", typeof(int));
        dtout.Columns.Add("Subject2", typeof(string)); dtout.Columns.Add("Marks2", typeof(int));
        dtout.Columns.Add("Subject3", typeof(string)); dtout.Columns.Add("Marks3", typeof(int));

        DataView view = new DataView(dt);
       DataTable distinctValues = view.ToTable(true, "ID", "Name" );
       foreach (DataRow row in distinctValues.Rows)
       {
           string ID = row["ID"].ToString();
           string Name = row["Name"].ToString();
          var newrow =  dtout.NewRow();
           DataRow[] array = dt.Select("ID=" + ID + " AND Name='" + Name + "'");
           newrow[0] = array[0][0];
           newrow[1] = array[0][1];
           int j = 2;
           for (int i = 0; i < array.Length; i++)
           { 
               
                   newrow[j] = array[i][2];
                   newrow[j +1] = array[i][3];
                   j++; j++;
               
           }
           dtout.Rows.Add(newrow);
             
       }
       
        


    }
}
 
Share this answer
 
v2
Comments
MurugappanCTS 10-Feb-14 7:53am    
Karthik Can u suggest Me a Sample Code.
Karthik_Mahalingam 10-Feb-14 7:54am    
sure, but you will be having only 2 subjects ? or it may increase
MurugappanCTS 10-Feb-14 7:56am    
May Increase upto 10 subjects
Karthik_Mahalingam 10-Feb-14 8:06am    
ok fine.wait
MurugappanCTS 10-Feb-14 8:22am    
Thanks For ur help karthik .....
try this

SQL
SELECT name, phy, che
FROM (
SELECT name, subject, marks
FROM marks) up
PIVOT (SUM(marks) FOR subject IN (phy, che)) AS pvt
 
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