簡體   English   中英

如何在Gridview頁腳中添加2行

[英]How to add 2 rows in Gridview footer

我正在使用網格來顯示沒有線索。 在那里,我必須顯示頁面明智總計和總計。 是否可以在頁腳中以2個不同的行顯示它? 給我一些建議。 我必須在網格中添加8列。

你可以通過很多方式做到這一點,但其中一種方法是使用TemplateField

這是gridview的格式(將您的內容放在單元格中)...

    <Columns>
        <asp:TemplateField>
            <FooterTemplate>
                <table width="100%">
                    <tr><td><asp:Literal runat="server" ID="ltField1" Text='<%# Bind("field1") %>'></asp:Literal></td>
                    </tr>
                    <tr><td>><asp:Literal runat="server" ID="ltField2" Text='<%# Bind("field2") %>'></asp:Literal></td>
                    </tr>
                </table>
            </FooterTemplate>

...

您必須通過繼承GridView類型來創建自定義GridView類。

namespace CustomControls
{
 public class CustomGridView : GridView
    {
        private string _pageTotal;

        public string PageTotal
        {
            get { return _pageTotal; }
            set { _pageTotal = value; }
        }

        private string _grandTotal;

        public string GrandTotal
        {
            get { return _grandTotal; }
            set { _grandTotal = value; }
        }

        public CustomGridView()
        {
        }

        protected override void OnRowCreated(GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.SetRenderMethodDelegate(CreateFooter);
            }
            base.OnRowCreated(e);
        }

        private void CreateFooter(HtmlTextWriter PageOutput, Control FooterContainer)
        {
            StringBuilder footer = new StringBuilder();
            footer.Append("<td>" + this._pageTotal  +"</td>");
            footer.Append("</tr>");
            footer.Append("<tr>");
            footer.Append("<td>" + this._grandTotal + "</td>");
            footer.Append("</tr>");
            PageOutput.Write(footer.ToString());          
        }
    }
}

然后使用'Register'頁面指令來引用您的自定義控件。

<%@ Register TagPrefix="cc" Namespace="CustomControls" %>

將控件添加到頁面,確保將ShowFooter設置為true。

<cc:CustomGridView ID="GridView1" ShowFooter="true"></cc:CustomGridView>

然后,您可以設置“PageTotal”和“GrandTotal”屬性。

GridView1.PageTotal = "5";
GridView1.GrandTotal = "10";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM