1

My markup is:

<asp:GridView ID="grvGroup" runat="server" AutoGenerateColumns="False" Width="100%" AllowPaging="True"
     PageSize="10" CssClass="mydatagrid"
     OnRowDataBound="grvGroup_RowDataBound" OnRowDeleting="grvGroup_RowDeleting" OnRowCommand="grvGroup_RowCommand"
     DataKeyNames="No" OnPageIndexChanging="grvGroup_PageIndexChanging" PagerStyle-HorizontalAlign="Center"
     PagerSettings-Mode="NumericFirstLast"
     PagerSettings-Visible="True">
   
    <Columns>
        <asp:ButtonField CommandName="Select" ButtonType="Button" ControlStyle-CssClass="Edit_btn" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" ItemStyle-Width="5px" HeaderStyle-Width="5px" />
        <asp:BoundField HeaderText="Sl.No." ItemStyle-Width="5px" HeaderStyle-Width="5px" DataField="No" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" />
        <asp:BoundField HeaderText="InvoiceNumber" ItemStyle-Width="100px" HeaderStyle-Width="100px" DataField="InvoiceNumber" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" />
        <asp:BoundField HeaderText="Vou Date" ItemStyle-Width="80px" HeaderStyle-Width="80px" DataField="CDate" HeaderStyle-HorizontalAlign="Left" DataFormatString="{0:dd/MM/yyyy}" HtmlEncode="False" ItemStyle-HorizontalAlign="Left" />
        <asp:BoundField HeaderText="BankName" ItemStyle-Width="130px" HeaderStyle-Width="130px" DataField="BankName" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" />
    </Columns>

    <EmptyDataRowStyle HorizontalAlign="Center" />
    <HeaderStyle CssClass="header" />
    <PagerStyle CssClass="pager" />
    <RowStyle CssClass="rows" />
    <AlternatingRowStyle CssClass="row-alt" />
</asp:GridView> 

This is my code behind file:

private void BindData(long refNo = 0, int pageIndex = 0)
{
    try
    {
        int pageSize = grvGroup.PageSize; // Get the page size

        DataSet Data = CommonClass.DSWriteToTable(
            _GetBank( refNo, pageIndex, pageSize)
        );

        if (Data != null && Data .Tables.Count > 1)
        {
            DataTable dt = Data .Tables[1];

            if (dt.Rows.Count > 0)
            {
                grvGroup.AllowPaging = true;
                grvGroup.PageSize = 10;
                grvGroup.Visible = true;
                grvGroup.PageIndex = pageIndex;
                grvGroup.DataSource = dt;
                grvGroup.DataBind();
            }
            else
            {
                grvGroup.Visible = false;
            }
            
            if (Data .Tables.Count > 2 && Data .Tables[2].Rows.Count > 0)
            {
                int totalRows = Convert.ToInt32(Data .Tables[2].Rows[0]["Counts"]);
                int totalPages = (int)Math.Ceiling((double)totalRows / pageSize);
                ViewState["TotalPages"] = totalPages;
             }
        }
    }
    catch (Exception ex)
    {
    }
}

While if the data set having more than 10 rows, it is showing row numbers under the grid if we use pagination set it is not showing any row number so the pagination is not happening

11
  • The question is not entirely clear to me, but it looks to me like Data.Tables[1] will only contain 10 rows since you send the pageSize and pageIndex parameter to DSWriteToTable. If so there is nothing to page for the gridview, that only works if the amount of rows bound to it is greater than PageSize. Commented Mar 19 at 12:15
  • yes the concept is same , but am passing the total rows number from the db to show {int totalRows = Convert.ToInt32(Data .Tables[2].Rows[0]["Counts"]); int totalPages = (int)Math.Ceiling((double)totalRows / pageSize); ViewState["TotalPages"] = totalPages;} by this also it is not showing the row number Commented Mar 19 at 12:45
  • As far as I remember you're not supposed to set the total number of pages. This is something the control should be calculating for you automatically. There is read-only property somewhere to see how many pages you're getting in total. Commented Mar 19 at 13:10
  • 1
    the GV pager reuqires the full data set to be sent to the GV EACH and EVERY time. What this means is the built in GV paging is really only approproate for about 300 rows or so, since EACH and EVERY time, you MUST send the full data table and ALL the rows to the GV. The GV pager thus only reduces bandwidth to the client side, it does not actually page data from the database server. Your routine looks to have its own paging built in, and thus you can't use the built in datapager. Since your routine looks to "already" be limiting the data, you have to dump the built in pager and use your own Commented Mar 19 at 17:13
  • ok, can you help to rewrite the code to obtain your concept, Commented Mar 20 at 4:11

1 Answer 1

5

To use the built in GridView paging, then your dataset has to return ALL rows, not just one page of data.

Hence, here is a working example:

Markup:

            <asp:GridView ID="GridView1" runat="server"
                AutoGenerateColumns="False" DataKeyNames="ID"
                CssClass="table table-hover" Width="800px"
                AllowPaging="true" PageSize="10"
                OnPageIndexChanging="GridView1_PageIndexChanging"
                >
                <Columns>
                    <asp:BoundField DataField="FirstName" HeaderText="FirstName" />
                    <asp:BoundField DataField="LastName" HeaderText="LastName" />
                    <asp:BoundField DataField="City" HeaderText="City" />
                    <asp:BoundField DataField="HotelName" HeaderText="HotelName" />
                    <asp:BoundField DataField="Description" HeaderText="Descripiton" />
                    <asp:TemplateField HeaderText="Edit">
                        <ItemTemplate>
                            <button id="cmdEdit" runat="server"
                                onserverclick="cmdEdit_ServerClick"
                                >
                                <span aria-hidden="true" class="fa  fa-pencil-square-o fa-lg"></span>
                            </button>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <PagerStyle CssClass="GridPager" VerticalAlign="Bottom" BorderWidth="2" />

            </asp:GridView>

And code behind to load the Gridview:

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                LoadData();
        }

        void LoadData()
        {
            string strSQL =
                @"SELECT * FROM tblHotelsA
                ORDER BY HotelName";

            GridView1.DataSource = General.MyRst(strSQL);
            GridView1.DataBind();

        }

And the code behind for the OnPageIndexChanging event is thus:

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            LoadData();
        }

And the result is now this:

enter image description here

--

Edit: using custom paging

===

You can also use custom paging, and this WILL allow you to use custom paging code.

Requirements:

You must be using asp.net 4.5 or later.

Set the GridView to with both AllowPaging="true" and also AllowcustomPaging = true.

And to set the number of rows, we use a new feature called:

VirtualItemCount

Here is a working example.

In this example, we have 6.7 million rows. Needless to say, we certainly don't want to have a "page number" control for each page, since with 6.7 million rows, and a page size of 16, then that would be 423273 pages!

So, this GridView:

            <asp:GridView ID="GVHistory" runat="server" AutoGenerateColumns="False"
                DataKeyNames="ID" CssClass="table table-hover" 
                AllowPaging="true"
                AllowCustomPaging="true"
                PageSize="16"
                OnPageIndexChanging="GVHistory_PageIndexChanging"
                Width="60%">
                <Columns>
                    <asp:BoundField DataField="DateStamp" HeaderText="Date" 
                        DataFormatString="{0:dd/MM/yyyy hh:mm tt}" ItemStyle-Width="160px" />
                    <asp:BoundField DataField="CompanyEmployeeID" HeaderText="Emp ID" ItemStyle-Width="80px" />
                    <asp:BoundField DataField="ComputerNetworkName" HeaderText="Computer Name" />
                    <asp:BoundField DataField="Note" HeaderText="Notes" />
                </Columns>
                <PagerTemplate>

                    <asp:Button ID="cmdFirst" runat="server" Text="First"
                        CommandName="Page" CommandArgument="First" 
                        CssClass="btn myshadow"
                        style="float:left"
                        />

                    <asp:Button ID="cmdPrev" runat="server" Text="Previous"
                        CommandName="Page" CommandArgument="Prev" 
                        CssClass="btn myshadow"
                        style="margin-left:10px;float:left"
                        />

                    <div style="float:left;margin-left:20px;margin-top:8px">
                        Page &nbsp; 
                        <asp:Label ID="lblPageNum" runat="server" Text=''> 
                        </asp:Label>
                        &nbsp;Of &nbsp; 
                        <asp:Label ID="lblPageCount" runat="server" 
                            Text=''>
                        </asp:Label>
                    </div>

                    <asp:Button ID="cmdNext" runat="server" Text="Next"
                        CommandName="Page" CommandArgument="Next" 
                        CssClass="btn myshadow"
                        style="margin-left:15px;float:left"
                        />

                    <asp:Button ID="cmdLast" runat="server" Text="Last"
                        CommandName="Page" CommandArgument="Last" 
                        CssClass="btn myshadow"
                        style="margin-left:10px;float:left"
                        />
                </PagerTemplate>
            </asp:GridView>

note the custom template. We simply dropped in 4 buttons (First, Previous, Next, and Last. Take CLOSE note of the command arguments used for the buttons.

So, now our code behind has to first "count" the total number of rows, and feed that number to the VirtualItemCount.

Note that that database pager code starts at page 1, but the GV starts at page 0.

Hence this code:

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dtRowCount = 
                    General.MyRst("SELECT COUNT(*) FROM dbo_ContactHistory",
                Properties.Settings.Default.AxisMIS);
                GVHistory.VirtualItemCount = (int)dtRowCount.Rows[0][0];
                LoadData(0);
            }
        }

        void LoadData(int PageNumber)
        {

            GVHistory.DataSource = GetData(PageNumber);
            GVHistory.DataBind();

            Label lblPage = (Label)GVHistory.BottomPagerRow.FindControl("lblPageNum");
            Label lblPageCount = (Label)GVHistory.BottomPagerRow.FindControl("lblPageCount");

            lblPage.Text = (PageNumber + 1).ToString();
            lblPageCount.Text = (GVHistory.VirtualItemCount / GVHistory.PageSize).ToString();

        }

        public DataTable GetData(int PageNum)
        {
            DataTable dtHistory = new DataTable();


            string strSQL =
                @"SELECT * FROM dbo_ContactHistory 
                ORDER BY dbo_ContactHistory.DateStamp
                OFFSET((@PageNumber - 1) * @RowsPerPage) ROWS
                FETCH NEXT @RowsPerPage ROWS ONLY";

            SqlCommand cmdSQL = new SqlCommand(strSQL);

            cmdSQL.Parameters.Add("@PageNumber", SqlDbType.Int).Value = PageNum + 1;
            cmdSQL.Parameters.Add("@RowsPerPage", SqlDbType.Int).Value = GVHistory.PageSize;

            dtHistory = General.MyRstP(cmdSQL, Properties.Settings.Default.AxisMIS);

            return dtHistory;

        }

        protected void GVHistory_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

            if (e.NewPageIndex >= 0)
            {
                GVHistory.PageIndex = e.NewPageIndex;
                LoadData(e.NewPageIndex);
            }

        }

And now we are paging data with 6.7 million rows, and the response time is instant.

Hence this:

enter image description here

Sign up to request clarification or add additional context in comments.

6 Comments

hai , it is good to load if the data is having less numbers , but if it is more numbers of data, this will delay with loading data in the grid.
Note that you can use what is called custom paging- I'll post an example to my answer here in a bit....
see my new edit - I provide a working "custom" pager example, and show how it is possible to use your existing pager idea.
i used same as you given above and tried, and am getting only first set of data and not showing the pagination. ( if i load all data it is showing with pagination), if i fetch first set of data it is not showing the pagination
You have to study the new example close. Note how the GridView now has both AllowPaging="true" and AllowCustomPaging="true". Note the introduction of using VirtualItemCount. So, all of these are "new" settings that you code has to deal with. Note how we have 6.7 million rows, and yet clicking on next is 100% instant response. So, the code as posted is working and tested code. So, with the new custom paging code, then we do NOT have to pull all rows from the database, but only a page of data (16 rows of data). This working example thus shows no real limit on database size.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.