在c#中拆分行和列值 [英] Split Rows and column values in c#

查看:102
本文介绍了在c#中拆分行和列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,



请帮助我。



DataTable价值:



 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 ........







我有列数。

但我希望首先显示0到5列,然后显示所有行的第6列到第5列。



我这样做了编码结果只有5列,而不是从第6列开始。





 Datatable dt = < span class =code-keyword> 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();

解决方案

如果您想要将数据打印在五列中:

 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 .........

然后你需要更多循环!

你需要一个外循环在列组上:

  for (startCol =  0 ; startCol <  dt.Columns.Count; startCol + =  5 
{
...
}

在其中,你需要你已经拥有的那对循环,但是从startCol值开始而不是零的j。 / blockquote>

你可以做这样的事情



 私法ate   static   void  splitTable(DataTable dataTable){

< span class =code-keyword> var splitAtColumn = 3 ;
var run = 0 ;
var columnsCount = dataTable.Columns.Count;

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

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

}

私人 静态 void 拆分(DataTable dataTable, int startWith, int thisColumnIndex, int columnsCount){

foreach (DataRow行 in dataTable.Rows){

var indexColumn = startWith;

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

Console.Write( \\\\ n< /跨度>);

}

}







splitAtColumn 会指定你要分割的位置...有很多可能的解决方案,选择你的风味:)


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.


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();

解决方案

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:

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.


You could do something like this

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


这篇关于在c#中拆分行和列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆