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 :)
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);
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);
}
}
}