table: SNAME CNAME GRADE
AA English 37
AA English 49
AA Maths 93
AA Maths 115
AA Maths 97
合并后:SNAME CNAME GRADE
English 37
49
AA 93
Maths 115
97
------------------------------------------------------------------------------------
int row=0;for (int i = 1; i < GridView1.Rows.Count; i++)
{
if (GridView1.Rows[i].Cells[0].Text == GridView1.Rows[i - 1].Cells[0].Text)
{
GridView1.Rows[row].Cells[0].RowSpan++;
GridView1.Rows[i].Cells[0].Visible = false;
}
else
{
GridView1.Rows[row].Cells[0].RowSpan++;
row = i;
}
}
GridView1.Rows[row].Cells[0].RowSpan++;
--------------------------------------------------------------------------------------
int userNameId;
string name = "";
int departNameId;
string depart = "";
int branchNameId;
string branch = "";
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//合并用户名
Label userName = e.Row.FindControl("lblUserName") as Label;
if (userName.Text == name)
{
if (GridView1.Rows[userNameId].Cells[0].RowSpan == 0)
{
GridView1.Rows[userNameId].Cells[0].RowSpan = 1;
}
GridView1.Rows[userNameId].Cells[0].RowSpan++;
e.Row.Cells[0].Visible = false;
}
else
{
name = userName.Text;
userNameId = e.Row.RowIndex;
}
//合并所属部门
Label departName = e.Row.FindControl("lblDepartName") as Label;
if (departName.Text == depart)
{
if (GridView1.Rows[departNameId].Cells[4].RowSpan == 0)
{
GridView1.Rows[departNameId].Cells[4].RowSpan = 1;
}
GridView1.Rows[departNameId].Cells[4].RowSpan++;
e.Row.Cells[4].Visible = false;
}
else
{
depart = departName.Text;
departNameId = e.Row.RowIndex;
}
//合并所属机构
Label branchName = e.Row.FindControl("lblBranchName") as Label;
if (branchName.Text == branch)
{
if (GridView1.Rows[branchNameId].Cells[5].RowSpan == 0)
{
GridView1.Rows[branchNameId].Cells[5].RowSpan = 1;
}
GridView1.Rows[branchNameId].Cells[5].RowSpan++;
e.Row.Cells[5].Visible = false;
}
else
{
branch = branchName.Text;
branchNameId = e.Row.RowIndex;
}
}
}