Paging In Grdiview using asp.net

Paging In Grdiview using asp.net


Step 1: Design web page .aspx

  <asp:GridView ID="gr" runat="server" AutoGenerateColumns="false" AlternatingRowStyle-BackColor="WhiteSmoke" Width="40%" ShowFooter="true" OnRowDataBound="gr_RowDataBound" FooterStyle-Font-Bold="true" AllowPaging="true" PageSize="7" OnPageIndexChanging="gr_PageIndexChanging">
                <HeaderStyle BackColor="#3366ff" Font-Bold="true" ForeColor="White" />
                <Columns>

                     <asp:TemplateField HeaderText="id">
                        <ItemTemplate>
                       
                               <%#Container.DataItemIndex+1 %>
                        </ItemTemplate>
                       
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Name">
                        <ItemTemplate>
                            <asp:Label ID="lbl_Name" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:Label ID="lbl_sum_msg" runat="server"></asp:Label>
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Name">
                        <ItemTemplate>
                            <asp:Label ID="lbl_Amount" runat="server" Text='<%#Eval("Amount") %>'></asp:Label>
                        </ItemTemplate>
                        <FooterTemplate>
                            <asp:Label ID="lbl_sum" runat="server"></asp:Label>
                        </FooterTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="Phone" HeaderText="Phone" />

                </Columns>
            </asp:GridView>

 Step 2: Logic on .CS page


   Double total = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind_gr();
        }

    }
    protected void gr_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            total += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "Amount"));


        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label lblamount = (Label)e.Row.FindControl("lbl_sum");
            lblamount.Text = total.ToString();

            Label lbl_MSG = (Label)e.Row.FindControl("lbl_sum_msg");
            lbl_MSG.Text = "Total";
        }

    }

    public void bind_gr()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Name");
        dt.Columns.Add("Amount");
        dt.Columns.Add("Phone");


        dt.Rows.Add("Kandy", "12", "8802***2564");
        dt.Rows.Add("Sam", "25", "545502*2544");
        dt.Rows.Add("Izhar", "32", "95802*2452");
        dt.Rows.Add("Vineet", "10.2", "88022564***");


        dt.Rows.Add("Kandy", "0.12", "8802***2564");
        dt.Rows.Add("Sam", "12", "545502*2544");
        dt.Rows.Add("Izhar", "215", "95802*2452");
        dt.Rows.Add("Vineet", "11.00", "88022564***");
        dt.Rows.Add("Kandy", "0.12", "8802***2564");
        dt.Rows.Add("Sam", "12", "545502*2544");
        dt.Rows.Add("Izhar", "215", "95802*2452");
        dt.Rows.Add("Vineet", "11.00", "88022564***");
        dt.Rows.Add("Kandy", "0.12", "8802***2564");
        dt.Rows.Add("Sam", "12", "545502*2544");
        dt.Rows.Add("Izhar", "215", "95802*2452");
        dt.Rows.Add("Vineet", "11.00", "88022564***");

        gr.DataSource = dt;
        gr.DataBind();

    }
    protected void gr_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gr.PageIndex = e.NewPageIndex;
        bind_gr();
    }

It will look like :-




Comments

Popular posts from this blog

Create and save QR code in asp.net with C#

Change text of RadGrid Header Dynamically

Telerik Radwindow Open on Button Click