Repeater 合并单元格

时间:2023-01-01 23:06:03

前途页面:

 <asp:Repeater ID="rptList" runat="server" OnPreRender="rptList_PreRender">
<ItemTemplate>
<tr>
<td align="center" runat="server" id="Num">
<%#Eval("Num")%>
</td>
<td align="center" runat="server" id="ProClassification">
<%#Eval("ProClassification")%>
</td>
<td align="center" runat="server" id="ProName">
<%#Eval("ItemName")%>
</td>
<td align="center" runat="server" id="Unit">
<%#Eval("Unit")%>
</td>
<td align="center" runat="server" id="Quantity">
<%#Eval("Quantity")%>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>

注意点:

1)绑定Repeater控件的OnPreRender方法

2)td单元格添加runat="server"和id属性

后台页面:
  

        protected void rptList_PreRender(object sender, EventArgs e)
{
string[] IDs = { "Num", "ProClassification", "ProName", "Unit", "Quantity" };
for (int i = rptList.Items.Count - ; i > ; i--)
{
for (int j = ; j < IDs.Length; j++)
{
//IDs[j]是需要合并列的列名
HtmlTableCell oCell_previous = rptList.Items[i - ].FindControl(IDs[j]) as HtmlTableCell;
HtmlTableCell oCell = rptList.Items[i].FindControl(IDs[j]) as HtmlTableCell;
if (oCell_previous != null && oCell != null)
{
oCell.RowSpan = (oCell.RowSpan == -) ? : oCell.RowSpan;
oCell_previous.RowSpan = (oCell_previous.RowSpan == -) ? : oCell_previous.RowSpan; if (oCell.InnerText == oCell_previous.InnerText)
{
if (oCell.InnerText.Trim()!="")//空单元格不执行合并操作
{
oCell.Visible = false;
oCell_previous.RowSpan += oCell.RowSpan;
}
}
}
} }
}