/// <summary>
/// 合并GridView单元格
/// </summary>
/// <param name="gridView">需要合并的GridView对象</param>
/// <param name="columnIndex">所要合并列的索引</param>
/// <param name="lblName">模板列对象的ID</param>
public static void MergeGrid(GridView gridView, int columnIndex, string lblName)
{
int i = 0;
string lastType = string.Empty;
int lastCell = 0;
if (gridView.Rows.Count > 0)
{
lastType = (gridView.Rows[0].Cells[columnIndex].FindControl(lblName) as Label).Text;
gridView.Rows[0].Cells[columnIndex].RowSpan = 1;
lastCell = 0;
}
for (i = 1; i < gridView.Rows.Count; i++)
{
if ((gridView.Rows[i].Cells[columnIndex].FindControl(lblName) as Label).Text == lastType)
{
gridView.Rows[i].Cells[columnIndex].Visible = false;
gridView.Rows[lastCell].Cells[columnIndex].RowSpan++;
}
else
{
lastType = (gridView.Rows[i].Cells[columnIndex].FindControl(lblName) as Label).Text.ToString();
lastCell = i;
gridView.Rows[i].Cells[columnIndex].RowSpan = 1;
}
}
}
使用时放到GridView1.DataBind();之后调用即可,如:
GridView1.DataSource = pager.GetTable();
GridView1.DataBind();
//合并单元格
MojoCube.Api.UI.AdminGridView.MergeGrid(GridView1, 1, "lblTitle");
效果: