#region 方法:合并Gridview行
/// <summary>
/// 合并GridView指定行单元格
/// </summary>
/// <param name="gv">GridView</param>
/// <param name="DataKeysName">DataKeys值(以此值为参照,如果此值相同,则将指定的单元格纵向合并)</param>
/// <param name="rowIndex">要合并的列索引(从0开始)</param>
protected void AppendGVRow(string DataKeysName, GridView gv, params int[] rowIndex)
{
try
{
int spanCount = 1;//合并的单元格跨的行数
int spanNum = -1;//用于确定需合并的起始行
for (int i = 0; i < rowIndex.Length; i++)
{
for (int j = 0; j < gv.Rows.Count - 1; j++)
{
if (gv.DataKeys[j][DataKeysName].ToString().Trim() == gv.DataKeys[j + 1][DataKeysName].ToString().Trim())//如果两行的text相同,则合并
{
if (spanNum == -1)
{
spanNum = j;//将需要合并的起始行保存到spanNum中
}
spanCount++;//合并的单元格跨的行数自增1
gv.Rows[j + 1].Cells[rowIndex[i]].Visible = false;//将下一行隐藏
gv.Rows[spanNum].Cells[rowIndex[i]].RowSpan = spanCount;
}
else
{
spanNum = -1;
spanCount = 1;
}
}
spanNum = -1;
spanCount = 1;
}
}
catch { }
}
#endregion