GridView、Repeater合并单元格
GridView、Repeater合并单元格
对于GridView、Repeater生成的表格一般都比较固定,但是有时候我们为了报表统计方便常把列名一样的单元格合并以达到易观察统计的效果,这样我们就需要对控件做必要的合并操作了,具体操作方法如下:
一、 GridView
前台代码:
一般格式……
后台代码:
for (int i = gridInfo.Rows.Count - 1; i > 0; i--)
{
if (gridInfo.Rows[i].RowType == DataControlRowType.DataRow)
{
TableCell tCell = gridInfo.Rows[i].Cells[0];
TableCell tCell_previous = gridInfo.Rows[i - 1].Cells[0];
tCell.RowSpan = (tCell.RowSpan == 0) ? 1 : tCell.RowSpan;
tCell_previous.RowSpan = (tCell_previous.RowSpan == 0) ? 1 : tCell_previous.RowSpan;
if (tCell.Text == tCell_previous.Text)
{
tCell.Visible = false;
tCell_previous.RowSpan += tCell.RowSpan;
}
}
}
{
if (gridInfo.Rows[i].RowType == DataControlRowType.DataRow)
{
TableCell tCell = gridInfo.Rows[i].Cells[0];
TableCell tCell_previous = gridInfo.Rows[i - 1].Cells[0];
tCell.RowSpan = (tCell.RowSpan == 0) ? 1 : tCell.RowSpan;
tCell_previous.RowSpan = (tCell_previous.RowSpan == 0) ? 1 : tCell_previous.RowSpan;
if (tCell.Text == tCell_previous.Text)
{
tCell.Visible = false;
tCell_previous.RowSpan += tCell.RowSpan;
}
}
}
二、Repeater
前台代码:
<asp:Repeater ID="rptAssetUsedPlan" runat="server">
<HeaderTemplate>
<table style="width: 98%; padding-top: 0px;" cellspacing="0px" cellpadding="0px"
align="center" class="gridtable">
<tr class="thbg" style="padding: 0 0 0 0">
<th align="left" style="width: 20%">
系统
</th>
<th align="left" style="width: 40%">
设备
</th>
<th align="left" style="width: 20%">
计划数量
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="width: 20%" align="left" runat="server" id="tdAssetparentcategoryname">
<%#Eval("Assetparentcategoryname")%>
</td>
<td align="left" style="width: 40%">
<%#Eval("Assetsubcategoryname")%>
</td>
<td align="left" style="width: 20%">
<asp:TextBox Width="100px" ID="txtPlanCount" runat="server" Text='<%#Eval("AssetCount") %>'></asp:TextBox>
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr>
<td style="width: 20%" align="left" runat="server" id="tdAssetparentcategoryname">
<%#Eval("Assetparentcategoryname")%>
</td>
<td style="width: 40%" align="left">
<%#Eval("Assetsubcategoryname")%>
</td>
<td align="left" style="width: 20%">
<asp:TextBox Width="100px" ID="txtPlanCount" runat="server" Text='<%#Eval("AssetCount") %>'></asp:TextBox>
</td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<HeaderTemplate>
<table style="width: 98%; padding-top: 0px;" cellspacing="0px" cellpadding="0px"
align="center" class="gridtable">
<tr class="thbg" style="padding: 0 0 0 0">
<th align="left" style="width: 20%">
系统
</th>
<th align="left" style="width: 40%">
设备
</th>
<th align="left" style="width: 20%">
计划数量
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="width: 20%" align="left" runat="server" id="tdAssetparentcategoryname">
<%#Eval("Assetparentcategoryname")%>
</td>
<td align="left" style="width: 40%">
<%#Eval("Assetsubcategoryname")%>
</td>
<td align="left" style="width: 20%">
<asp:TextBox Width="100px" ID="txtPlanCount" runat="server" Text='<%#Eval("AssetCount") %>'></asp:TextBox>
</td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr>
<td style="width: 20%" align="left" runat="server" id="tdAssetparentcategoryname">
<%#Eval("Assetparentcategoryname")%>
</td>
<td style="width: 40%" align="left">
<%#Eval("Assetsubcategoryname")%>
</td>
<td align="left" style="width: 20%">
<asp:TextBox Width="100px" ID="txtPlanCount" runat="server" Text='<%#Eval("AssetCount") %>'></asp:TextBox>
</td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
后台代码:
数据绑定之后添加如下代码:
// 合并单元格
for (int i = rptAssetUsedPlan.Items.Count - 1; i > 0; i--)
{
HtmlTableCell oCell_previous = rptAssetUsedPlan.Items[i - 1].FindControl("tdAssetparentcategoryname") as HtmlTableCell;
HtmlTableCell oCell = rptAssetUsedPlan.Items[i].FindControl("tdAssetparentcategoryname") as HtmlTableCell;
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;
}
}
for (int i = rptAssetUsedPlan.Items.Count - 1; i > 0; i--)
{
HtmlTableCell oCell_previous = rptAssetUsedPlan.Items[i - 1].FindControl("tdAssetparentcategoryname") as HtmlTableCell;
HtmlTableCell oCell = rptAssetUsedPlan.Items[i].FindControl("tdAssetparentcategoryname") as HtmlTableCell;
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;
}
}
效果图: