我正在使用GridView准备打印报告,现在我以brut格式(两列(ID,名称))填充数据库中的数据
ID名称
==========
1 x1
2 x3
3 x4
4 x8
经理要求在两列中打印数据
ID名称ID名称
======================
1 x1 3 x4
2 x3 4 x8
代码:
static DataTable GetData()
{
DataTable dt = new DataTable();
dt.Columns.Add(
new DataColumn("ID", typeof(Int32))
);
dt.Columns.Add(
new DataColumn("Name", typeof(string)));
for (int i = 1; i <= 200; i++)
{
DataRow Row = dt.NewRow();
Row["ID"] = i;
Row["Name"] = "Name " + i.ToString();
dt.Rows.Add(Row); } return dt;
}
}
有什么解决方案可以将数据表拆分为两列。
致谢
答案 0 :(得分:0)
方法1:
DataTable dtFull = GetData();
DataTable newDt = new DataTable();
newDt.Columns.Add("ID", typeof(String));
newDt.Columns.Add("Name", typeof(String));
newDt.Columns.Add("ID", typeof(String));
newDt.Columns.Add("Name", typeof(String));
for (int i = 0; i < dtFull.Rows.Count; i++)
{
DataRow newrow = newDt.NewRow();
if (i * 2 < dtFull.Rows.Count)
{
newrow["ID"] = dtFull.Rows[i * 2][0].ToString();
newrow["Name"] = dtFull.Rows[i * 2][1].ToString();
newDt.Rows.Add(newrow);
}
if (i*2+1 < dtFull.Rows.Count)
{
newDt.Rows[i][2] = dtFull.Rows[i * 2 + 1][0].ToString();
newDt.Rows[i][3] = dtFull.Rows[i * 2 + 1][1].ToString();
}
}
答案 1 :(得分:0)
方法2(“步骤参数”页面行数):
DataTable dtFull = GetData();
DataTable newDt = new DataTable();
newDt.Columns.Add("ID", typeof(String));
newDt.Columns.Add("Name", typeof(String));
newDt.Columns.Add("ID", typeof(String));
newDt.Columns.Add("Name", typeof(String));
bool exit = true;
int pagecount = 10;
int i = 0;
int j = 0;
int k = 0;
int m = 0;
while (exit)
{
for (j = 0; (j<pagecount);j++)
{
DataRow newrow = newDt.NewRow();
newrow["ID"] = dtFull.Rows[i][0].ToString();
newrow["Name"] = dtFull.Rows[i][1].ToString();
newDt.Rows.Add(newrow);
i++;
m++;
if (i >= dtFull.Rows.Count)
{
exit = false;
break;
}
}
if (exit)
{
k = m - pagecount;
for (j = 0; (j < pagecount); j++)
{
newDt.Rows[m - pagecount + j][2] = dtFull.Rows[i][0].ToString();
newDt.Rows[m - pagecount + j][3] = dtFull.Rows[i][1].ToString();
i++;
if (i >= dtFull.Rows.Count)
{
exit = false;
break;
}
}
}
}