locked
ASP.NET GridView Binding RRS feed

  • Question

  • User891861947 posted

    Hi All,

    This is more of a design question:

    SENARIO:

    If I have a DataSet for 30000 records, and I am binding this Data with the GridView.

    The Method that gets the data from the database is GetMyData()


    What will be the best way to load this data into the grid:

    1) Get all 30000 records from the database, store it into the DataSet, bind the DataSet with the PAGING Enabled GridView.

    2) First get the size of the page of the GridView, then get only the number of records First page can support, attach the DataSet to the GridView (Paging Enabled), and then for each page get the data when required.


    I think the First approach will put an unnessary load on the database and the network bandwidth if the user can only see 100 records per page. then why should we get 30000 records and display only 100. Also when paging is enabled, and all the dataset is produced in code behind and attached to the GridView, then for each page number click we need to do this:

    GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)

    {

    GridView.DataSource = GetMyData();
    GridView = e.NewPageIndex;
    GridView.DataBind();

    }

    so basically i am displaying 100 records but getting all 30000 records every time the user clicks a page.


    Am i doing it the wrong way, or is it really done this way?


    if it is done this way, then i think the 2nd approach is better. But the problem with the second approah is there will be a lots of manuall work to do, such as keeping track of how much data has been loaded, from where to start the next 100 records (when quering the database) etc....

    Please suggest the best way to get and bind lots of data to the grid view control in asp.net in the light of above senario.




    Regards.


    Thursday, November 4, 2010 9:48 AM

Answers

  • User-1590642642 posted

    It's pretty much as you describe in option 2, and yes, it is complicated with lots of bits and pieces as you say.   I would perform logic in .net using the page size and which page the user is looking at to determine a MIN and MAX rownumber, and pass those rownumbers to my stored proc to get only those rows.   So if I have 100 rows per page, and the user goes to page 5, I query for rows 401-500. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 4, 2010 11:52 AM
  • User-952121411 posted

    What will be the best way to load this data into the grid:

     

    Dynamically loading only the data needed upon paging when working with that many records might be best.  If you use an ASP.NET ObjectDataSource control, you can set paging at the DataSource level which will only get the data needed to display. This would limit the amount of data brought back. The tradeoff is going to the database every time the GridView is paged. But you have to ask yourself, how often is someone using paging and how many times will they page. If on average they only page 1-3 times, dynamically loading will probably work. If they page 50 times, having to go to the database each time, then it is not worth the overhead. In this case fetching all of the records and paging from the cached source is fastest. Some of it depends on the nature of how the GridView you have built with the 30,000 records is going to be used and paged.

    For more information, check out the following (2) links:

    Implement Paging using ObjectDataSource with GridView:

    http://www.codeproject.com/KB/aspnet/PagingWithODS.aspx

    Using Caching on an Object Data Source and Making it Unique Per User: (solution for caching and quick paging when all data is loaded)

    http://allen-conway-dotnet.blogspot.com/2010/05/using-caching-on-object-data-source-and.html

    Hope this helps! Smile

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 4, 2010 12:04 PM

All replies

  • User-1590642642 posted

    I have always handled this by putting the paging in the query, meaning I only get the records I am going to display.   However, I've never done any benchmark tests or anything that would show why this is the best way.   But I hate to waste bandwidth on my sql server, so I always do whatever will reduce the load there. 

    Thursday, November 4, 2010 10:00 AM
  • User891861947 posted

    Also wanted to add:


    if option 1 is used (which i think also is not one of the options that TabAlleman has recommended) then how do we populate grid in AJAX timer control, we can not call GetMyData() after every five sec and bind the results to GridView, so there must be some better way of doing it.


    Option 2 can do it, but again its complicated with lots of bits and pieces.

    @TabAlleman: can you explain your method of doing this. Is it the way i said in option 2, or your design is different?


    Thanks...

    Thursday, November 4, 2010 11:17 AM
  • User-1590642642 posted

    It's pretty much as you describe in option 2, and yes, it is complicated with lots of bits and pieces as you say.   I would perform logic in .net using the page size and which page the user is looking at to determine a MIN and MAX rownumber, and pass those rownumbers to my stored proc to get only those rows.   So if I have 100 rows per page, and the user goes to page 5, I query for rows 401-500. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 4, 2010 11:52 AM
  • User-952121411 posted

    What will be the best way to load this data into the grid:

     

    Dynamically loading only the data needed upon paging when working with that many records might be best.  If you use an ASP.NET ObjectDataSource control, you can set paging at the DataSource level which will only get the data needed to display. This would limit the amount of data brought back. The tradeoff is going to the database every time the GridView is paged. But you have to ask yourself, how often is someone using paging and how many times will they page. If on average they only page 1-3 times, dynamically loading will probably work. If they page 50 times, having to go to the database each time, then it is not worth the overhead. In this case fetching all of the records and paging from the cached source is fastest. Some of it depends on the nature of how the GridView you have built with the 30,000 records is going to be used and paged.

    For more information, check out the following (2) links:

    Implement Paging using ObjectDataSource with GridView:

    http://www.codeproject.com/KB/aspnet/PagingWithODS.aspx

    Using Caching on an Object Data Source and Making it Unique Per User: (solution for caching and quick paging when all data is loaded)

    http://allen-conway-dotnet.blogspot.com/2010/05/using-caching-on-object-data-source-and.html

    Hope this helps! Smile

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 4, 2010 12:04 PM