日期:2014-05-18 浏览次数:20593 次
原始样式
姓名 照片 性别 出生年月 其它列1 其它列2 其它列3
A P M XXXX O1 O2 O3
A P M XXXX O4 O5 O6
A P M XXXX O7 O8 O9
B P2 F YYYY 10 11 12
B P2 F YYYY 13 14 15
B P2 F YYYY 16 17 18
需求样式
姓名 照片 性别 出生年月 其它列1 其它列2 其它列3
O1 O2 O3
A P M XXXX O4 O5 O6
O7 O8 O9
10 11 12
B P2 F YYYY 13 14 15
16 17 18
/// <summary>
/// 合并行(合并的列要用Label控件)
/// </summary>
/// <param name="gvw">需要合并的GridView</param>
/// <param name="sCol">要合并的列(从0开始)</param>
/// <param name="controlName">控件名称</param>
public static void MergeRows(GridView gvw, int col, string controlName)
{
for (int rowIndex = gvw.Rows.Count - 2; rowIndex >= 0; rowIndex--)
{
GridViewRow row = gvw.Rows[rowIndex];
GridViewRow previousRow = gvw.Rows[rowIndex + 1];
Label row_lbl = row.Cells[col].FindControl(controlName) as Label;
Label previousRow_lbl = previousRow.Cells[col].FindControl(controlName) as Label;
if (row_lbl != null && previousRow_lbl != null)
{
if (row_lbl.Text == previousRow_lbl.Text)
{
row.Cells[col].RowSpan = previousRow.Cells[col].RowSpan < 1 ? 2 : previousRow.Cells[col].RowSpan + 1;
previousRow.Cells[col].Visible = false;
}
}
}
}
------解决方案--------------------
这样的页面最好用table来做,然后ajax去后台取数据这样的灵活性大一点。
------解决方案--------------------
一般都用Table 好操作
------解决方案--------------------
protected void Page_Load(object sender, EventArgs e)
{
//...
GridView1.DataSource = dt;
GridView1.DataBind();
GroupName(0);
GroupName(1);
GroupName(3);
GroupSex();
}
public void GroupName(int col)
{
TableCell oldName = GridView1.Rows[0].Cells[col];
for (int i = 1; i < GridView1.Rows.Count; i++)
{
TableCell Name = GridView1.Rows[i].Cells[col];
if (oldName.Text == Name.Text)
{
Name.Visible = false;
if (oldName.RowSpan == 0)
{
oldName.RowSpan = 1;
}
oldName.RowSpan++;
oldName.VerticalAlign = VerticalAlign.Middle;
}
else
{
oldName = Name;
}
}
}
public void GroupSex()
{
TableCell oldName = GridView1.Rows[0].Cells[0];
TableCell oldSex = GridView1.Rows[0].Cells[2];
for (int i = 1; i < GridView1.Rows.Count; i++)
{
TableCell Name = GridView1.Rows[i].Cells[0];
TableCell Sex = GridView1.Rows[i].Cells[2];
if (oldName.Text == Name.Text && oldSex.Text == Sex.Text)
{
Sex.Visible = false;
if (oldSex.RowSpan == 0)
{