If a procedure returns 5000 (Five Thousand) rows and I want bind it with a Asp.net Grid View. Neither it won’t be good approach to call all the rows and bind it with grid and then on grid view Pageindex see all the data or this approach to strike the DB at each Pageindex change. So can anybody give me good answer.
Normally your GridView is only presentation. So you only have to query the correct page to display in the GridView. If you go the the next page you query the db for the next page.
This article will give you directions on how to do this with a datasource:
MSDN - Tutorial 25: Efficiently Paging Through Large Amounts of Data
Efficient Server Side Paging with the ASP.NET GridView Control
Session["dt"] = DataTable; //after you get table from procedure, assign it to session
Then in pageload, every time bind grid view to datatable saved in session, you call it from DB just first time
protected void Page_Load(object sender, EventArgs e)
{
GridView.DataSource = Session["dt"];
GridView.DataBind();
}
Related
I have populated a GridView using the following method:
List<MyObject> items = new List<MyObject>();
// here I am filling the list using SQL
PanelGridView.DataSource = items; //fill GridView with objects, this works when NOT using paging
PanelGridView.DataBind();
With this, and paging disabled, I have a fully populated GridView. However, when I turn on paging, the first page is filled, but all subsequent pages are empty. How can I make sure all of the items are accounted for, and divided properly among the pages (given the page size I've specified)?
EDIT: I forgot to include this code:
protected void PanelGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
PanelGridView.PageIndex = e.NewPageIndex;
PanelGridView.DataBind();
}
Make sure you databind the grid after changing the page and also make sure to set the page index to the new page index. This is done in the PageChanging event handler.
i have a gridview in my page and a datatable with 1000 rows in it .now i can bind gridview with datatable intwo different ways
bind datatable directly to gridview.
create another datatable with first ten rows and bind it to grid. on the next page request bind next 10 rows to gridview.
now my question is, does my second aproach really help me to improve perfomance of the page?
There are two ways to improve the performance in this case:
Enable paging in your GridView
Advantage: easy to implement, you don't need to change your DataSource
Disadvantage: You need to select all records from dbms
Page the DataSource itself(f.e. SQL-Server)
Advantage: Very fast and scalable
Disadvantage: More time-consuming to implement
With only 1000 rows i would recommend to enable paging in GridView.
I have a gridview in an updatepanel where also my dropdownlist is. From the Trigger of the dropdownlist I am refreshing my gridview with the slected value. All that is working fine. The problem is that I am also displaying the gridview row count on the page which is also inside the updatepanel. The update seems to be happening to it, one selection too late.
protected void Drop_Change(object sender, EventArgs e)
{
String Value = AjaxDrop.SelectedValue;
GridView1.SelectParameters["Target"].DefaultValue=Value;
RowCount.InnerText = GridView1.Rows.Count.ToString();
}
I think its happening one selection behind because the parameter updates the gridview rowcount too late for the RowCount value to have it, what is a work around to get the actual value after parameter passed. Only way I can think of is using javascript and I wonder if that would even work. My desired solution would be to keep it all on server side though.
At the time you're calling this, the GridView hasn't done it's databinding, therefore the value is not valid. Move your RowCount.InnerText updating to somewhere that gets executed after the DataBinding event (e.g. Page_OnPreRender), or force the DataBinding to occur before you update the row count.
It's worth noting that GridView.Rows.Count isn't a reliable source of information if you are using GridView paging - as this will be the number of rows on one page, even if there are more rows returned by the query.
hi i have simple gridView with AllowPaging set to true, i bind data to it as follow:
RulesGridView.DataSource = GetData();
RulesGridView.DataBind();
where
public static IEnumerable GetData()
{
return from gc in context.Current.SampleTable
where gc.SameField == sameValue
select new
{
example = SampleData
};
}
In sql SampleTable i have 4000 rows, my question is , why gridView is taking all data from this table, instead of only first page of gridview? i checked with sql profiler , and that is really true, displaying just first page on gridview couse downloading all gridview pages. Can i change it to take from sql only so many what is enough to display data on first page of DataGrid, and then if user click second page , gridview will ask for another data?
Because you are binding method which simple returns all records.
Possible solution: You have to modify your GetData method to accept parameter which will mark pagination and apply it in inner query. Then at each "pageNext/Prev" event, you just modify that parameter to new value.
Edit:
Codeproject custom paging for gridView
Aspsnippets about pagination
I like this discussion http://www.west-wind.com/weblog/posts/211.aspx; it may give you additional insight or decide that it is time to pull every hair from Gates.
I have confusion about GridView in ASP.NET.
How does the GridView exactly work?
I mean when we bind data to gridview with 100 records through GridView1.DataBind();
I have set Pageindexchanging Event and I set the Pagesize = "40" and AllowPaging="True" Then....
Now the interesting part is begins What happens when i Click on next page index of GridView is it. Once again go to the database and fetch the data.. or gridview creates its own dataset and fetch data from that dataset or anything different than this...
And one more thing is how the Sorting works in GridView?
The simple answer is yes, the GridView is simply a view placed over a DataGrid, and all the parameters specified affect how the GridView is rendered for the user. Since we're dealing with the stateless web, you will have to rebind the data.
One trick for this is that if your dataset isn't too large, you can store the DataTable in the user's session and simply retrieve it from there, saving a trip to the database. If you are dealing with a large amount of data, then you'll want to look into options for having your SQL queries function in a "paged" format so you only retrieve the rows you intend to display.
With Paging and Sorting, they serve as an event to respond to in which you resort your data and rebind to the grid for presentation.
The advantage of the GridView is that you do have a central object with a lot of functionality built in that you can use for rapid deployment. When you get a hand of how the sorting, paging, row commands, and other things work, you can do some really great things in a small amount of code.
It depends what your data source is, if your datasource supports paging, then it will fetch only the records it needs to show that page, if it does not, it will get all the records and discard the one you don't want.
For both examples, it will talk to the datasource everytime you change page.
See here for more details:
http://msdn.microsoft.com/en-us/library/5aw1xfh3.aspx