You must Sign In to post a response.
  • Category: ASP.NET

    How Paging is working in ASP.net Gridview

    Hi,

    In my interview, Interviewer asked how the paging is fetch the data from database. For example, 50 row of records should be show 5 records per page, so there is total 10 pages. While loading the page the first 5 record will be shown from database. when we click the 2nd, 3rd pages respectively... it will shown next 5 records each... how this will fetch in 2nd and 3rd page like wise end of the record.

    whether it will go back to database and fetch the data after click the 2nd page? please clarify me
  • #766417
    There are lot of ways to do the paging.
    1. In the gridview you can enable the paging. If you enable the paging the gridview will take care the paging. You do not worry about the paging logic. You just assign the enter data source to the grid view. It will take care.

    2. You can handle it in the database. Write the stored procedure with required parameters. And also add parameters like page number, number of records to be fetch. Based in the inputs you can fetch the records and return back the records and also total number of records. Now you can create user control for paging, you can do your own design. From the user control pass the page number and number of records need to show.

    The SP will return the records you can show the records in your page. If you do this using ajax call it will be looks very nice.

    By Nathan
    Direction is important than speed

  • #766418
    The simple and correct answer is, the GridView control will perform paging by getting all of the data records from the source, displaying only the records for the current page, and discarding the rest.
    The ASP.NET GridView control has a built-in paging capability that supports basic paging functionality. You can use the default paging user interface (UI) or create a custom paging interface.
    1.If the GridView control is bound to a data source control that is capable of returning a single page of data when requested, the GridView control will take advantage of that capability directly. The number of rows requested may vary depending on the number of rows per page specified by the PageSize property, and whether the data source supports getting the total row count

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #766433
    Hi,

    As per my understand the post in GridView by default itself there is a concept called as Paging, using that property of GridView we can set the no of records in a page.

    No need to go to the database for each and every page click, get the all records from database and store it in one datatable and bind the same to gridview it will internally perform action of Paging once you set those paging properties in gridview.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #766449
    Hi,
    No it will not go back to database and fetch the data after click the 2nd page. You just have to reassign the datasource to gridview on PageIndexChanging() event.
    Please find demo code:

    .aspx page:
    <-asp:GridView runat="server" ID="gvDemo" AutoGenerateColumns="true" PageSize="5" AllowPaging="true" OnRowDataBound="gvDemo_RowDataBound" OnPageIndexChanging="gvDemo_PageIndexChanging">
    <-Columns>
    <-asp:TemplateField>
    <-ItemTemplate>
    <-asp:DropDownList runat="server" ID="DemoDDL" AutoPostBack="false" Text='<%# Eval("hMy") %>' />
    <-/ItemTemplate>
    <-/asp:TemplateField>
    <-/Columns>
    <-/asp:GridView>
    .cs page:

    DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
    IDataReader reader = null;
    SqlConnection con = new SqlConnection("Data Source=XYZ;Initial Catalog=DBNAME;User ID=USERNAME;Password=USERPWD");
    con.Open();
    SqlDataAdapter a = new SqlDataAdapter("SELECT * FROM [TableName]", con);
    a.Fill(dt);
    gvDemo.DataSource = dt;
    gvDemo.DataBind();
    }

    protected void gvDemo_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    DropDownList ddl = (DropDownList)e.Row.FindControl("DemoDDL");
    ddl.DataSource = dt;
    ddl.DataTextField = "ColumnaName";
    ddl.DataValueField = "ColumnaName";
    ddl.DataBind();
    }
    }

    protected void gvDemo_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
    gvDemo.PageIndex = e.NewPageIndex;
    gvDemo.DataSource = dt;
    gvDemo.DataBind();
    }

  • #766886
    There are several way you can do it the concept on demand on load like radtelerik contorls are evolved with this. and you can do it fetching all records at a time and filtering the records according to that this is called custom paging. No it will not go back to database and fetch the records set the page size attribute and allow paging is true that's it.
    SRI RAMA PHANI BHUSHAN KAMBHAMPATI


  • Sign In to post your comments