26
2012
06

asp.net(c#) 中如何将Repeater部分合并单元格

前台代码:
 <table cellpadding="0" cellspacing="0" border="1" bordercolor="black" width="100%" style="border-collapse:collapse; border-style:solid;">

        <asp:Repeater runat="server" ID="rptInfo">

            <HeaderTemplate>

            <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>

            </FooterTemplate>

        </asp:Repeater> 
</table>

后台代码:

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;
     if(oCell!=null && oCell_previous!=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;
         }

    }
}
 

 

用法把合并方法放到邦定之后;就OK了放在数据绑定之后,如下:
this.rptInfo.DataSource = dt;
this.rptInfo.DataBind();

// 合并单元格
for (int i = rptInfo.Items.Count - 1; i > 0; i--)
{
HtmlTableCell oCell_previous = rptInfo.Items[i-1].FindControl("tdName") as HtmlTableCell;  //要合并的列td赋值一个ID
HtmlTableCell oCell = rptInfo.Items[i].FindControl("tdName") 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;
}

}
}

« 上一篇下一篇 »

评论列表:

1.美国空间购买  2012/6/28 10:04:29 回复该留言
来博主这取经了。
2.蝴蝶毛线  2012/6/30 11:12:38 回复该留言
看不懂,太专业了

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。