html文件:
<asp:Repeater ID="rptEmployee" runat="server">
<HeaderTemplate>
<table border="" cellpadding="" cellspacing=""><tr><td>省份</td><td>城市</td></tr>
</HeaderTemplate>
<ItemTemplate>
<tr><td runat="server" id="divProName"><%#Eval("FirstName")%></td><td><%#Eval("LastName")%></td></tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
后台文件:
C#版:
private void BindData()
{
this.rptEmployee.DataSource = DBHelper.GetExecuteDataSet("select FirstName,LastName from dbo.Employees group by firstName,LastName", null);
this.rptEmployee.DataBind();
for (int i = this.rptEmployee.Items.Count - ; i > ; i--)
{
HtmlTableCell oCell_pre = this.rptEmployee.Items[i - ].FindControl("divProName") as HtmlTableCell;
HtmlTableCell oCell = rptEmployee.Items[i].FindControl("divProName") as HtmlTableCell;
oCell.RowSpan = oCell.RowSpan == - ? : oCell.RowSpan;
oCell_pre.RowSpan = oCell_pre.RowSpan == - ? : oCell_pre.RowSpan;
if (oCell.InnerHtml==oCell_pre.InnerHtml)
{
oCell.Visible = false;
oCell_pre.RowSpan += oCell.RowSpan;
}
}
}
VB版:
Private Sub BindData()
Me.rptEmployee.DataSource = DBHelper.GetExecuteDataSet("select EmployeeID, LastName, FirstName from Employees order by firstName,EmployeeID desc", Nothing)
Me.rptEmployee.DataBind()
For i As Integer = Me.rptEmployee.Items.Count - To Step -
Dim cell_pre, cell As HtmlTableCell
cell_pre = Me.rptEmployee.Items(i - ).FindControl("divProName") '上一行
cell = Me.rptEmployee.Items(i).FindControl("divProName") '当前行
If cell.RowSpan = - Then
cell.RowSpan = '当前行没有合并行,则当行不用合并
Else
cell.RowSpan = cell.RowSpan '当行的合并行,等于当前的已经合并的行数
End If
If cell_pre.RowSpan = - Then
cell_pre.RowSpan =
Else
cell_pre.RowSpan = cell_pre.RowSpan
End If
If (cell_pre.InnerText = cell.InnerText) Then
cell.Visible = False
cell_pre.RowSpan += cell.RowSpan
End If
Next
End Sub