/// <summary>
/// 合并GridView中某列相同信息的单元格
/// </summary>
/// <param name="GridView1">表格对象</param>
/// <param name="cellNum">待合并的列数(从0开始计算)</param>
public static void GroupRows(GridView GridView1, int cellNum)
{
int i = 0, rowSpanNum = 1;
while (i < GridView1.Rows.Count - 1)
{
GridViewRow gvr = GridView1.Rows[i];
for (++i; i < GridView1.Rows.Count; i++)
{
GridViewRow gvrNext = GridView1.Rows[i];
if (gvr.Cells[cellNum].Text == gvrNext.Cells[cellNum].Text)
{
gvrNext.Cells[cellNum].Visible = false;
rowSpanNum++;
}
else
{
gvr.Cells[cellNum].RowSpan = rowSpanNum;
rowSpanNum = 1;
break;
}
if (i == GridView1.Rows.Count - 1)
{
gvr.Cells[cellNum].RowSpan = rowSpanNum;
}
}
}
}
/// <summary>
/// 合并GridView中某ItemTemplate列相同信息的单元格
/// </summary>
/// <param name="GridView1">表格对象</param>
/// <param name="cellNum">待合并的列数(从0开始计算)</param>
/// <param name="ControlName">表格中HyperLink的对象名</param>
public static void GroupRowsItem(GridView GridView1, int cellNum, string ControlName)
{
int i = 0, rowSpanNum = 1;
while (i < GridView1.Rows.Count - 1)
{
GridViewRow gvr = GridView1.Rows[i];
for (++i; i < GridView1.Rows.Count; i++)
{
GridViewRow gvrNext = GridView1.Rows[i];
if (((HyperLink)gvr.Cells[cellNum].FindControl(ControlName)).Text == ((HyperLink)gvrNext.Cells[cellNum].FindControl(ControlName)).Text)
{
gvrNext.Cells[cellNum].Visible = false;
rowSpanNum++;
}
else
{
gvr.Cells[cellNum].RowSpan = rowSpanNum;
rowSpanNum = 1;
break;
}
if (i == GridView1.Rows.Count - 1)
{
gvr.Cells[cellNum].RowSpan = rowSpanNum;
}
}
}
}
public static void GroupRowsItem(GridView GridView1, int cellNum, string ControlName, int[] cellNums)
{
int i = 0, rowSpanNum = 1;
while (i < GridView1.Rows.Count - 1)
{
GridViewRow gvr = GridView1.Rows[i];
for (++i; i < GridView1.Rows.Count; i++)
{
GridViewRow gvrNext = GridView1.Rows[i];
if (((HyperLink)gvr.Cells[cellNum].FindControl(ControlName)).Text == ((HyperLink)gvrNext.Cells[cellNum].FindControl(ControlName)).Text)
{
gvrNext.Cells[cellNum].Visible = false;
for (int row = 0; row < cellNums.Length; row++)
{
gvrNext.Cells[cellNums[row]].Visible = false;
}
rowSpanNum++;
}
else
{
gvr.Cells[cellNum].RowSpan = rowSpanNum;
for (int row = 0; row < cellNums.Length; row++)
{
gvr.Cells[cellNums[row]].RowSpan = rowSpanNum;
}
rowSpanNum = 1;
break;
}
if (i == GridView1.Rows.Count - 1)
{
gvr.Cells[cellNum].RowSpan = rowSpanNum;
for (int row = 0; row < cellNums.Length; row++)
{
gvr.Cells[cellNums[row]].RowSpan = rowSpanNum;
}
}
}
}
}