一下内容为绑定到4个字段的代码,方法哈了点,直接重新构建了一个数据源然后绑定的,不知道有没有更好的方法= =#
比较乱,演示个思路而已,随笔备忘
- DataSet ds = new DataSet();
ds.ReadXml("d://a.xml");
DataTable dt = ds.Tables[0];
DataTable dtCur = new DataTable();
DataColumn dc1 = new DataColumn("c1");
DataColumn dc2 = new DataColumn("c2");
DataColumn dc3 = new DataColumn("c3");
DataColumn dc4 = new DataColumn("c4");
DataColumn dca = new DataColumn("ca");
DataColumn dcb = new DataColumn("cb");
DataColumn dcc = new DataColumn("cc");
DataColumn dcd = new DataColumn("cd");
dtCur.Columns.Add(dc1);
dtCur.Columns.Add(dca);
dtCur.Columns.Add(dc2);
dtCur.Columns.Add(dcb);
dtCur.Columns.Add(dc3);
dtCur.Columns.Add(dcc);
dtCur.Columns.Add(dc4);
dtCur.Columns.Add(dcd);
DataRow drCur = dtCur.NewRow();
for (int i = 0; i < dt.Rows.Count; i++)
{
int x = i % 4;
switch (x)
{
case 0:
drCur[0] = dt.Rows[i][0].ToString();
drCur[1] = dt.Rows[i][1].ToString();
break;
case 1:
drCur[2] = dt.Rows[i][0].ToString();
drCur[3] = dt.Rows[i][1].ToString();
break;
case 2:
drCur[4] = dt.Rows[i][0].ToString();
drCur[5] = dt.Rows[i][1].ToString();
break;
case 3:
drCur[6] = dt.Rows[i][0].ToString();
drCur[7] = dt.Rows[i][1].ToString();
dtCur.Rows.Add(drCur);
drCur = dtCur.NewRow();
break;
}
if (i == dt.Rows.Count - 1 && x != 3)
{
dtCur.Rows.Add(drCur);
}- }
this.dataGridView1.DataSource = dtCur;
取值
- DataTable dt = new DataTable();
- DataColumn dc1 = new DataColumn("dc1");
- DataColumn dc2 = new DataColumn("dc2");
- dt.Columns.Add(dc1);
- dt.Columns.Add(dc2);
- DataRow ddr = dt.NewRow() ;
- foreach (DataGridViewRow dr in dataGridView1.Rows)
- {
- for (int i = 0; i < 8; i++)
- {
- int x = i % 2;
- switch (x)
- {
- case 0:
- ddr = dt.NewRow();
- ddr[0] = dr.Cells[i].Value;
- break;
- case 1:
- ddr[1] = dr.Cells[i].Value;
- dt.Rows.Add(ddr);
- break;
- }
- }
- }
- this.dataGridView2.DataSource = dt;