Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

Please help me.

DataTable Value:

      col1 col2 col3 col4 col5 col6 col7 ........
row   1    2    3    4    5    6    7    ........
      11   322  32   45   6    65   76   ........
      10   20   30   40   50   60   70   ........
      20   21   12   13   14   15   16   ........



I have number of columns.
but i want to display 0 to 5 columns with all rows first,then from 6th to 5 columns with all rows.

I have done this coding result is coming for only 5 columns, not from 6th onwards.


C#
Datatable dt = new Datatable();

               for (int i = 0; i < dt.rows.Count; i++)
               {

                   for (int j = 0; j < dt.columns.count ; j++)
                   {
                       Console.Write(dt.rows[i][j] + "  ");

                   }

                   Console.WriteLine();
               }



           Console.Read();
Posted
Updated 9-Jul-13 23:15pm
v2
Comments
StM0n 10-Jul-13 5:02am    
I'm not quite sure what your target format should look like... could you give an example?
connect2manas 10-Jul-13 5:12am    
output should be
1 2 3 4 5
11 322 32 45
10 20 30 40 50
20 21 12 13 14


6 7 8 9 10
65 76 5 66 77 5
60 70 80 90 100


like this..

If you want to print your data always in five columns:
 1   2  3  4  5
11 322 32 45  6
10  20 30 40 50
20  21 12 13 14
 6  7 .........
65 76 .........
60 70 .........
15 16 .........
Then you will need more loops!
You need an outer loop which works on groups of columns:
C#
for (startCol = 0; startCol < dt.Columns.Count; startCol += 5)
   {
   ...
   }
Within that, you will need the pair of loops you already have, but with "j" starting from the startCol value instead of zero.
 
Share this answer
 
You could do something like this

C#
private static void splitTable(DataTable dataTable) {

    var splitAtColumn = 3;
    var run           = 0;
    var columnsCount  = dataTable.Columns.Count;

    while (splitAtColumn * run < columnsCount) {
        Split(
            dataTable,
            splitAtColumn * run,
            splitAtColumn * ++run,
            columnsCount);

        Console.WriteLine("----------------");
    }

}

private static void Split(DataTable dataTable, int startWith, int thisColumnIndex, int columnsCount) {

    foreach(DataRow row in dataTable.Rows) {

        var indexColumn = startWith;

        while (indexColumn < thisColumnIndex && indexColumn < columnsCount) {
            Console.Write(row[indexColumn++] + " ");
        }

        Console.Write("\r\n");

    }

}



splitAtColumn would specify where you want to split... there are many possible solutions, pick your flavour :)
 
Share this answer
 
v4

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