-1

i have DataTable which has only one column, how do I convert it to four columns.

my DataTable looks like below

Column1
1
2
3
4
5
6
7
8

and I want to convert it as showing below

Column1 Column2 Column3 Column4
1       2       3       4
5       6       7       8
Usman Ali
  • 778
  • 8
  • 21
  • Have you tried anything? – Prashant Pimpale Jan 13 '19 at 08:18
  • 3
    @UsmanAli - Then please try something. If it doesn't work then please post your code so that we can help. – Enigmativity Jan 13 '19 at 08:21
  • @Enigmativity, i have no idea where to start from – Usman Ali Jan 13 '19 at 08:22
  • Do you need it to be programmaticaly generated or you know already which values will reside in the columns?. Please give some context of your problem: how do you represent the data, how do you populate the column, etc. As the other SO fellows suggested, please give a try before (otherwise people would need to make different assumptions - generally this is a bad idea) – Shmwel Jan 13 '19 at 08:24
  • yes, you can write down the requirement on the paper, define steps to follow and go throw it. [Look here](https://stackoverflow.com/questions/1774498/how-to-iterate-through-a-datatable) – Prashant Pimpale Jan 13 '19 at 08:25
  • @UsmanAli A quick online search turned up: [Adding columns to a DataTable](https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/dataset-datatable-dataview/adding-columns-to-a-datatable) and [deleting rows from a datatable](https://stackoverflow.com/questions/5648339/deleting-specific-rows-from-datatable). Try something and then come back if you get stuck! – Rufus L Jan 13 '19 at 08:25
  • @UsmanAli - Sure you know where to start. Start by declaring a `DataTable` and populating it - show us that code. Declare a second `DataTable` and populate that with your desired output. Give us that too. That gives us something to test against. Then just try to convert it - even by copy field by field. You can surely do that. – Enigmativity Jan 13 '19 at 08:27

1 Answers1

1

Try following :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt1 = new DataTable();
            dt1.Columns.Add("Column1", typeof(int));
            dt1.Rows.Add(1);
            dt1.Rows.Add(2);
            dt1.Rows.Add(3);
            dt1.Rows.Add(4);
            dt1.Rows.Add(5);
            dt1.Rows.Add(6);
            dt1.Rows.Add(7);
            dt1.Rows.Add(8);

            DataTable dt2 = new DataTable();
            dt2.Columns.Add("Column1", typeof(int));
            dt2.Columns.Add("Column2", typeof(int));
            dt2.Columns.Add("Column3", typeof(int));
            dt2.Columns.Add("Column4", typeof(int));


            for (int i = 0; i < dt1.Rows.Count; i += 4)
            {
                dt2.Rows.Add(new object[] { dt1.Rows[i].Field<int>("Column1"), dt1.Rows[i + 1].Field<int>("Column1"), dt1.Rows[i + 2].Field<int>("Column1"), dt1.Rows[i + 3].Field<int>("Column1") });
            }

        }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20