repeater控件合并单元格

代码:

<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">&nbsp;<%#Eval("num"%></td><td id="tdname" runat="server">&nbsp;<%#Eval("name")%></td><td>&nbsp;<%#Eval("firsttime)%></td> <td>&nbsp;<%#Eval("department")%></td> <td>&nbsp;<%#Eval("title")%></td> <td>&nbsp;<%#Eval("people")%></td> <td>&nbsp;<%#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;
                            }
                        }
                    }
                }                
            }
        }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奋斗---现在进行时

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值