C# .NET - how to convert row to column in C#.net using array or any way

Asked By vijayan c on 31-Jan-11 01:57 AM
hi Rohan Dave please clear me
i am VIJAYAN C
I HAVE  A TABLE LIKE THIS
TABLE NAME : PROFILE
rollno  name      class   code    mark  
300    balaji       BSC    dddd    70
301    barathi      BSC    dddd    52
302    babu       BSC    dddd    80
300    balaji     BSC    dede   88
302    babu        BSC    dede    50
301   barathi      BSC    dede    55
302    babu     BSC    aabb   86
300    balaji       BSC    aabb   70
301    barathi      BSC    aabb    70
303    deva       BCA    cccc     86
304    guru       BCA    cccc     90
305    vijay      BCA    cccc     85
303    deva       BCA    eeee    86
304    guru       BCA    eeee    56
305   vijay      BCA     eeee   95


when i enter  BSC in a textbox and click the submit button
I WANT TO LIKE THIS THE THE TABLE
rollno  name   class   aabb dddd dede  
300    balaji   BSC   70    70   88
301    barathi  BSC   70   52    55
302    babu   BSC    86   80   50

But when i enter BCA in a textbox and click the submit button
I WANT TO LIKE THIS THE THE TABLE
rollno  name   class   cccc   eeee   
303    deva     BCA   86     86
304    guru      BCA    90      56
305   vijay     BCA    85      95
i am waiting ..........
please help me its very needful to me
thank you very very much as advance
Reena Jain replied to vijayan c on 31-Jan-11 03:11 AM
hi,

if you want to use transpose concept then here is a good link for you

http://www.codeproject.com/KB/database/TransposeTable.aspx

hope this will help you
Mihir Soni replied to vijayan c on 31-Jan-11 03:38 AM
Hello here is the function through which you can interchange the row/ columns of the datatable

private DataTable GenerateTransposedTable(DataTable inputTable)
{
   DataTable outputTable = new DataTable();
 
   // Add columns by looping rows
 
   // Header row's first column is same as in inputTable
   outputTable.Columns.Add(inputTable.Columns[0].ColumnName.ToString());
 
   // Header row's second column onwards, 'inputTable's first column taken
   foreach (DataRow inRow in inputTable.Rows)
   {
     string newColName = inRow[0].ToString();
     outputTable.Columns.Add(newColName);
   }
 
   // Add rows by looping columns     
   for (int rCount = 1; rCount <= inputTable.Columns.Count - 1; rCount++)
   {
     DataRow newRow = outputTable.NewRow();
 
     // First column is inputTable's Header row's second column
     newRow[0] = inputTable.Columns[rCount].ColumnName.ToString();
     for (int cCount = 0; cCount <= inputTable.Rows.Count - 1; cCount++)
     {
       string colValue = inputTable.Rows[cCount][rCount].ToString();
       newRow[cCount + 1] = colValue;
     }
     outputTable.Rows.Add(newRow);
   }
 
   return outputTable;
}

Just store the values in datatable and then just pass that datatable in above function you will get new table as transpose of that table