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 runat="server" ID="rptInfo">
<HeaderTemplate>
<table cellpadding="0" cellspacing="0" border="1" bordercolor="black" width="100%" style="border-collapse:collapse; border-style:solid;">
<tr>
<td>科室名称</td>
<td>出诊医生</td>
<td>职称</td>
<td>诊病人数</td>
<td>挂号价格</td>
<td>挂号金额</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td runat="server" id="tdDepName"><%#Eval("dep_name")%></td>
<td><%#Eval("doctor_name")%></td>
<td><%#Eval("title_name")%></td>
<td><%#Eval("register_person")%></td>
<td><%#Eval("price")%></td>
<td><%#Eval("total_money")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<HeaderTemplate>
<table cellpadding="0" cellspacing="0" border="1" bordercolor="black" width="100%" style="border-collapse:collapse; border-style:solid;">
<tr>
<td>科室名称</td>
<td>出诊医生</td>
<td>职称</td>
<td>诊病人数</td>
<td>挂号价格</td>
<td>挂号金额</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td runat="server" id="tdDepName"><%#Eval("dep_name")%></td>
<td><%#Eval("doctor_name")%></td>
<td><%#Eval("title_name")%></td>
<td><%#Eval("register_person")%></td>
<td><%#Eval("price")%></td>
<td><%#Eval("total_money")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
后台代码:
for (int i = rptInfo.Items.Count - 1; i > 0; i--)
{
HtmlTableCell oCell_previous = rptInfo.Items[i-1].FindControl("tdDepName") as HtmlTableCell;
HtmlTableCell oCell = rptInfo.Items[i].FindControl("tdDepName") 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;
}
}
{
HtmlTableCell oCell_previous = rptInfo.Items[i-1].FindControl("tdDepName") as HtmlTableCell;
HtmlTableCell oCell = rptInfo.Items[i].FindControl("tdDepName") 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;
}
}
三、效果
科室名称 |
出诊医生 |
职称 |
诊病人数 |
挂号价格 |
挂号金额 |
保健科 |
孙思邈 |
副高 |
1 |
33.0000 |
33.0000 |
华佗 |
正高 |
10 |
30.0000 |
300.0000 |
|
黄欧 |
正老 |
6 |
18.0000 |
108.0000 |
|
免费号 |
免费 |
6 |
0.0000 |
0.0000 |
|
小计 |
副高 |
23 |
0.0000 |
441.0000 |
|
保健门诊 |
超级用户 |
正高 |
2 |
10.0000 |
20.0000 |
超级用户 |
正老 |
2 |
11.0000 |
22.0000 |
|
超级用户 |
副高 |
2 |
21.0000 |
42.0000 |
|
超级用户 |
正高 |
5 |
43.0000 |
215.0000 |
|
李时珍 |
正老 |
1 |
1.0000 |
1.0000 |