日期:2014-05-18  浏览次数:21039 次

第一次发帖,求解答
DataTable dt = ds.Tables[0];
dt中数据结构为

  金额A 金额B 金额C 部门
  11 12 13 01
  21 22 23 01
  31 32 33 01
  41 42 43 02
  51 52 53 02

我想抽出金额数据,顺序为
11,21,31,12,22,32,13,23,33,41,51,42,52,43,53

foreach (DataRow dr in dt.Rows) 
{
//怎么写
}

谢谢了。

------解决方案--------------------
C# code
var dtbl = new DataTable();
for (int i = 0; i < 4; i++)
{
    dtbl.Columns.Add(i.ToString());
}
dtbl.Rows.Add("11", "12", "13", "01");
dtbl.Rows.Add("21", "22", "23", "01");
dtbl.Rows.Add("31", "32", "33", "01");
dtbl.Rows.Add("41", "42", "43", "02");
dtbl.Rows.Add("51", "52", "53", "02");

var row = 3;
var column = 3;
var lines = dtbl.Select();
var rarr = new string[lines.Length * column];

int rem = lines.Length % row;
int mr = lines.Length - rem;
for (int i = 0; i < lines.Length; i++)
{
    var arr = lines[i].ItemArray;
    var r = i < mr ? row : rem;
    for (int j = 0; j < column; j++)
    {
        rarr[i / row * row * column + j * r + i % row] = arr[j].ToString();
    }
}

var result = string.Join(",", rarr);
// 11,21,31,12,22,32,13,23,33,41,51,42,52,43,53