gridview单元格合并解决方法


先上效果图!

实现代码!

 1    protected void Page_Load(object sender, EventArgs e)
 2     {
 3 //...
 4         GridView1.DataSource = dt;
 5         GridView1.DataBind();
 6         GroupName(0);
 7         GroupName(1);
 8         GroupName(3);
 9         GroupSex();
10     }
11     public void GroupName(int col)
12     {
13         TableCell oldName = GridView1.Rows[0].Cells[col];
14         for (int i = 1; i < GridView1.Rows.Count; i++)
15         {
16             TableCell Name = GridView1.Rows[i].Cells[col];
17             if (oldName.Text == Name.Text)
18             {
19                 Name.Visible = false;
20                 if (oldName.RowSpan == 0)
21                 {
22                     oldName.RowSpan = 1;
23                 }
24                 oldName.RowSpan++;
25                 oldName.VerticalAlign = VerticalAlign.Middle;
26             }
27             else
28             {
29                 oldName = Name;
30             }
31         }
32     }