我们通常会遇到GridView Repeater 相邻列,值相同时合并的情况如图
今天把实现方法写一下
思路:因为GridView我们的值我们可以从后台自己获取。所以我们在得到要合并的列时,循环比较行列上下行之间的值,如果相等,改变一行Rowspan值,隐藏行一行
GridView实现:
/// <summary>
/// 合并行
/// </summary>
/// <param name="gvw"> 需要合并的GridView</param>
/// <param name="sCol">sCol要合并开始列(从0开始)</param>
/// <param name="eCol">eCol要合并的结束列</param>
/// <param name="refCol">refCol以哪一列作为标准</param>
public static void MergeRows(GridView gvw, int sCol, int eCol, int[] refCol)
{
for (int rowIndex = gvw.Rows.Count - 2; rowIndex >= 0; rowIndex--)
{
//获取相邻行的值row进行比较
GridViewRow row = gvw.Rows[rowIndex];
GridViewRow previousRow = gvw.Rows[rowIndex + 1];
for (int i = sCol; i < eCol + 1; i++)
{
{
//判断是否需要合并
bool isSame = true;
for (int j = 0; j < refCol.Length; j++)
{
if (row.Cells[refCol[j]].Text != previousRow.Cells[refCol[j]].Text)
{
isSame = false;
break;
}
}
if (!isSame)
{
continue;
}
//修改相邻两行的RowSpan的值
row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan < 1 ? 2 : previousRow.Cells[i].RowSpan + 1;
previousRow.Cells[i].Visible = false;
}
}
}
}
Repeater 思路,因为repeater 的值后台无法取到,所以每个值所在的td 中设置ID="xxx" runat="server",来获取前台的值
页面代码:
<asp:Repeater ID="rptBillItem" runat="server"> <HeaderTemplate> <tr class="datarow"> <td> 果品等级<br /> GRADE </td> <td> 单箱重量<br /> WEUGHT/CTN </td> <td> 规格<br /> SIZE </td> <td> 箱数<br /> QUANTITY </td> <td> 用箱种类<br /> BRAND </td> <td> 备 注<br /> REMARK </td> </tr> </HeaderTemplate> <ItemTemplate> <tr class="datarow" > <td runat="server" id="grade"> <%#Eval("grade") %> </td> <td runat="server" id="weuqht"> <%#Eval("weuqht") %> </td> <td runat="server" id="size"> <%#Eval("size") %> </td> <td runat="server" id="quantity"> <%#Eval("quantity") %> </td> <td runat="server" id="brand"> <%#Eval("brand") %> </td> <td runat="server" id="remark" > <%#Eval("remark") %> </td> </tr> </ItemTemplate> </asp:Repeater>
后台方法:
/// <summary> /// 合并行 /// </summary> /// <param name="gvw"> 需要合并的Repeater</param> /// <param name="refCol">refCol以哪一列作为标准</param> /// <param name="con">要合并的列名ID</param> public static void MergeRows(Repeater gvw, string[] refCol, string[] con) { //循环所以的行 for (int rowIndex = gvw.Items.Count - 2; rowIndex >= 0; rowIndex--) { for (int i = 0; i < con.Length; i++) { //获得该行上一行的数据 HtmlTableCell oCell_previous = gvw.Items[rowIndex].FindControl(con[i]) as HtmlTableCell; HtmlTableCell oCell = gvw.Items[rowIndex + 1].FindControl(con[i]) as HtmlTableCell; oCell.RowSpan = (oCell.RowSpan == -1) ? 1 : oCell.RowSpan; oCell_previous.RowSpan = (oCell_previous.RowSpan == -1) ? 1 : oCell_previous.RowSpan; { bool isSame = true; //循环要合并的列 for (int j = 0; j < refCol.Length; j++) { HtmlTableCell refColtilel_previous = gvw.Items[rowIndex].FindControl(refCol[j]) as HtmlTableCell; HtmlTableCell refColtile = gvw.Items[rowIndex + 1].FindControl(refCol[j]) as HtmlTableCell; if (refColtile.InnerText != refColtilel_previous.InnerText) { if (con[i] != "remark") { isSame = false; break; } } } if (!isSame) { continue; } //如果两列值相等则合并 if (oCell.InnerText == oCell_previous.InnerText) { oCell.Visible = false; oCell_previous.RowSpan += oCell.RowSpan; } } } } }