代码:
<table width="100%" id="mytable" >
<tr align="center">
<th>num</th><th>nam</th><th>firsttime</th> <th>department</th> <th>title</th> <th>people</th> <th>notes</th>
</tr>
<asp:Repeater ID="Repeater1" runat="server" onprerender="Repeater1_PreRender">
<ItemTemplate>
<tr><td id="tdnum" runat="server"> <%#Eval("num") %></td><td id="tdname" runat="server"> <%#Eval("name")%></td><td> <%#Eval("firsttime)%></td> <td> <%#Eval("department")%></td> <td> <%#Eval("title")%></td> <td> <%#Eval("people")%></td> <td> <%#Eval("notes")%></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table
代码1:(标准)
protected void dataRepeater_PreRender(object sender, EventArgs e)
{
for (int i = dataRepeater.Items.Count - 1; i > 0; i--)
{
//type是需要合并列的列名
第一列
HtmlTableCell oCell_previous = dataRepeater.Items[i - 1].FindControl("type") as HtmlTableCell;
HtmlTableCell oCell = dataRepeater.Items[i].FindControl("type") as HtmlTableCell;
if (oCell_previous != null && oCell != null)
{
oCell.RowSpan = (oCell.RowSpan == -1) ? 1 : oCell.RowSpan;
oCell_previous.RowSpan = (oCell_previous.RowSpan == -1) ? 1 : oCell_previous.RowSpan;
if (oCell.InnerText == oCell_previous.InnerText)
{
oCell.Visible = false;
oCell_previous.RowSpan += oCell.RowSpan;
}
}
第二列
HtmlTableCell nameprevious = dataRepeater.Items[i - 1].FindControl("times") as HtmlTableCell;
HtmlTableCell name = dataRepeater.Items[i].FindControl("times") as HtmlTableCell;
if (nameprevious != null && name != null)
{
name.RowSpan = (name.RowSpan == -1) ? 1 : name.RowSpan;
nameprevious.RowSpan = (nameprevious.RowSpan == -1) ? 1 : nameprevious.RowSpan;
if (name.InnerText == nameprevious.InnerText)
{
name.Visible = false;
nameprevious.RowSpan += name.RowSpan;
}
}
}
}
代码2:两列一致才合并
protected void dataRepeater_PreRender(object sender, EventArgs e)
{
for (int i = dataRepeater.Items.Count - 1; i > 0; i--)
{
//tcid是需要合并列的列名
HtmlTableCell oCell_previous = dataRepeater.Items[i - 1].FindControl("type") as HtmlTableCell;
HtmlTableCell oCell = dataRepeater.Items[i].FindControl("type") as HtmlTableCell;
if (oCell_previous != null && oCell != null)
{
oCell.RowSpan = (oCell.RowSpan == -1) ? 1 : oCell.RowSpan;
oCell_previous.RowSpan = (oCell_previous.RowSpan == -1) ? 1 : oCell_previous.RowSpan;
if (oCell.InnerText == oCell_previous.InnerText)
{
oCell.Visible = false;
oCell_previous.RowSpan += oCell.RowSpan;
//只有第一列的相同才考虑第二列的
HtmlTableCell nameprevious = dataRepeater.Items[i - 1].FindControl("times") as HtmlTableCell;
HtmlTableCell name = dataRepeater.Items[i].FindControl("times") as HtmlTableCell;
if (nameprevious != null && name != null)
{
name.RowSpan = (name.RowSpan == -1) ? 1 : name.RowSpan;
nameprevious.RowSpan = (nameprevious.RowSpan == -1) ? 1 : nameprevious.RowSpan;
if (name.InnerText == nameprevious.InnerText)
{
name.Visible = false;
nameprevious.RowSpan += name.RowSpan;
}
}
}
}
}
}