粗略的写了一下,你看是否你要的。可以再优化一下。
private DataTable GetDt()
{
string str = "asd|asd|asd|asd|asdasd|asad|asd|asd|asd|asd|asdasd|asad|asd|asd|asd|asd|asdasd|asad";
DataTable dtTemp = new DataTable();
DataRow drTemp;
dtTemp.Columns.Add("Col0", typeof(string));
dtTemp.Columns.Add("Col1", typeof(string));
dtTemp.Columns.Add("Col2", typeof(string));
dtTemp.Columns.Add("Col3", typeof(string));
dtTemp.Columns.Add("Col4", typeof(string));
dtTemp.Columns.Add("Col5", typeof(string));
string[] strs = str.Split(new Char[] { '|' });
for (int j = 1; j <= (strs.Length / 6); j++)
{
drTemp = dtTemp.NewRow();
for (int i = 0; i < 6; i++)
{
drTemp["Col" + i] = strs[j * i];
}
dtTemp.Rows.Add(drTemp);
}
return dtTemp;
}
string[] tmpStrArr = tmpStr.Split('|');
DataTable dt = new DataTable();
int colCount = 6;
for (int i = 0; i < colCount; i++) {
dt.Columns.Add(new DataColumn("col"+i.ToString(), typeof(string)));
}
for (int i = 0; i < tmpStrArr.Length / colCount; i++)
{
string[] tmpArr = new string[colCount];
Array.Copy(tmpStrArr, i * colCount, tmpArr, 0, colCount);
dt.Rows.Add(tmpArr);
}