Friday, May 17, 2013

GridView Paging in Asp.Net Custom Format


 I have a web  project in which I had to show  specific number of records and that that specific number of records  will display in each next and previous button . I have used List<> for the datasource  for GridView.


Source Code :-
<asp:GridView ID="GridViewItems" runat="server" Width="100%" DataKeyNames="Item_ID"
 OnPageIndexChanging="GridViewItems_PageIndexChanging"   PageSize="12" >


<asp:TemplateField>
                                    <FooterTemplate>
                                        <asp:Label ID="LblRecordCount" Text="" runat="server" Style="text-align: center;
                                            margin-left: -290px; font-size: 11px"></asp:Label>
                                        <asp:Button ID="BtnNext" runat="server" Text=">>" OnClick="BtnNext_Click" CssClass="ButtonPlain" />
                                        <asp:Button ID="BtnBack" runat="server" Text="<<" ToolTip="Previous" Style="margin-left: -117px"
                                            OnClick="BtnBack_Click" CssClass="ButtonPlain" />
                                    </FooterTemplate>
                                </asp:TemplateField>


C# Code :-
protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
               
                if (IsPostBack == false)
                {
                    GetAllItem();
                    GetRecord();
                }
            }
            catch (Exception ex)
            {
            }
        }

    public void GetAllItem()
        {
            try
            {
                var ItemList = (from it in LstDetails()
                                orderby it.Item_ID descending
                                select it;
                if (ItemList.ToList().Count > 0)
                {
                    ViewState["RowCount"] = ItemList.ToList().Count;
                    GridViewItems.Visible = true;
                    GridViewItemVariation.Visible = false;
                    GridViewItems.DataSource = ItemList.ToList();
                    GridViewItems.DataBind();
                }
            }
            catch (Exception ex)
            {

            }
        }

      public void GetRecord()
        {
            int InitialCount = 1;
            int totalcount = int.Parse(ViewState["RowCount"].ToString());
            int num = GridViewItems.PageIndex + 1;
            int x = num * GridViewItems.PageSize;
            int z = x - GridViewItems.PageSize + 1;
            Button MyBtnBack = (Button)GridViewItems.FooterRow.FindControl("BtnBack");
            Button MyBtnNext = (Button)GridViewItems.FooterRow.FindControl("BtnNext");
            Label Mylabel = (Label)GridViewItems.FooterRow.FindControl("LblRecordCount");
            if (x > totalcount)
            {
                Mylabel.Text = z + " - " + totalcount + "  of " + totalcount;
                Mylabel.Visible = true;
            }
            else
            {
                Mylabel.Text = z + " - " + x + "  of " + totalcount;
                Mylabel.Visible = true;

            }
        }

 public void BtnNext_Click(object sender, EventArgs e)
        {
            if (Convert.ToString(ViewState["cc"]) == "")
            {
                GridViewItems.PageIndex = 1;
                ViewState["cc"] = GridViewItems.PageIndex;
            }
            else
            {
                if (GridViewItems.PageIndex < (int.Parse(ViewState["RowCount"].ToString()) / GridViewItems.PageSize))
                {
                    GridViewItems.PageIndex = int.Parse(ViewState["cc"].ToString()) + 1;
                    ViewState["cc"] = GridViewItems.PageIndex;
                }
            }


            GetAllItemCommon();
            GetRecord();
        }

        public void BtnBack_Click(object sender, EventArgs e)
        {
            if (Convert.ToString(ViewState["cc"]) != "")
            {

                if (GridViewItems.PageIndex > 0)
                {
                    GridViewItems.PageIndex = int.Parse(ViewState["cc"].ToString()) - 1;
                    ViewState["cc"] = GridViewItems.PageIndex;
                }
            }

            GetAllItemCommon();
            GetRecord();
        }


No comments:

Post a Comment