ASP.NET MVC 3 WebGrid paging issue - c#

My data access layer returns collection with rows for single page and total number of rows.
Unfortunately WebGrid component does not allow to specify total number of rows or total number of pages (these properties are read-only).
Has anyone had to deal with this issue before?

You can use the Bind method on the WebGrid to tell the grid to use server side paging.
grdv.Bind(myData, rowCount=10000, autoSortAndPage=False)
Setting autoSortAndPage to false tells the grid that myData is just a segment of the data. It will show all rows of this data regardless of your page size setting. Pager will be built using the rowCount you pass in and not the number of records in myData.

EDIT: I see what your question is now. Check out this article for not using the WebGrid.
Paging with WebGrid
From this page, it looks like you can specify rows per page.
var grid = new WebGrid(source, rowsPerPage : 25);
And this page (look at line 9 from the first code block).

rowsPerPage is only settable through the constructor. This was done to keep the helper simple and avoid handling complex states. Total rows comes from the data source.

Related

How can you fetch the rows for multiple pages in a single call of server in kendo grid?

In kendo grid paging, when we click the page number it calls the server and returns a set of data. Because of this the performance is very low. Instead, I need to call the server and get 50 rows at a time and the rows should be displayed in five pages in kendo grid.
Can anyone help with this?
please try to give ServerOperation(false) for the grid it will take all the data at a time

How can I do sorting on multiple columns in WebGrid?

I am using WebGrid in my ASP.NET MVC 3 (C#) application. I want to do sorting on multiple columns(like we can do in tablesorter).
Is it possible in WebGrid? If so, how can I do it?
The WebGrid Helper provides sorting by default.
So, you'd get sorting and paging by default when you write a simple instantiation like below:
var myGrid = new WebGrid(Model);
myGrid.GetHtml();
Quoted from a dead link:
Out of the box, the WebGrid provides these functions by doing Get requests and passing in a few values over the query string. The WebGrid will see these values in the new request, and adjust the output accordingly. This means that the WebGrid expects the entire set of data in each view in order to generate the paging properly.
This means that you would have performance issues for larger datasets as you would need to pull all of it into memory at once for the Helper to compute paging and sorting metrics correctly.

How to improve the performance of a Gridview that contains 10000 data records in c# asp.net?

I had an interview Question .
Q. how to improve performance of a Gridview contains 100000 Records of data(Datasource might be XML or DB) in c# asp.net ?
I was just blinking as i had no answer for this .Please Give me the solution for this. If possible please give one demo.
No gridview should contain 100000 records.
Your data source may contain 10 times that number of records, it doesn't matter. If you present a user more than 100 records on screen, it would be difficult to work with that data.
Select a datasource that supports paging for the gridview, that way you are only working with the number of records to be displayed on screen.
For a database you can use something like SqlDataSource.
The default XmlDataSource does not support paging, in such cases the grid will have all records in memory and handle paging itself. For xml with large number of records consider using an ObjectDataSource, where you can implement paging yourself.
If you have a choice, always select a data source that supports paging.
To fix such a GridView, I think a good approach would be to:
Add paging (as others have already pointed out)
Add filtering/searching
Add a cap/limit for maximum number of rows shown
The filtering + max limit for results is (in my opinion) a good way to keep the size of the grid manageable. If the max limit is hit using some specified set of filters (=search terms) you could show a message about it to the user and encourage him/her to specify a more specific search if necessary.
As stated before, a gridview should not hold so many rows.
In your interview, they wanted to see if you can say that its bad implemented rather then to check your programming skill.
But if one insist for good performance, I would suggest to use Repeater instead of Gridview.
Cheers,
Ran

Showing records on grid efficiently

i have a third component grid that i am using on my page.
It displays 20 records in one page.
Also i am using images to display certain columns.
Like comments , attachements have clickable images for every row.
the problem is.. everytime i load my page.. the logic right now is.. it goes in the database.. checks for every row in the table to see if the comments are added or is there attachment.. and accordingly disables or enable the image of that particular record(row)
Now this takes too many database hits and processing time increases.
Can u tell me any other way to do this.?
You could add a column "Number of comments" and "Number of attachments" to your rows.
You should enable some sort of VirtualMode in your grid ( every grid should have this function in some way ) in order to feed just the item the user actually see. This is a good practice always.
Irrespective of grid, you can build some sort of object which collectively holds the textual information that you need to show and some more flags which tell you about the image and attachment. You bind the collection of this custom object to your grid.
Use the flags in this object to put different images in your columns.
HTH

Manually setting a GridView's PageCount when DataSource doesn't return full result set?

I'm trying to figure out ASP.NET's GridView pagination mechanics so I can use the framework's native functionality instead of my company's home-brewed manual pagination routines which take a lot of work to implement.
I've figured out everything except how get the GridView's PageCount property to work with our web services. Currently, our web services return the total record count like the following:
public object[] GetStuffMethod(int pageNum, int recordsPerPage, out int totalRecords)
This works fine with a GridView, however the documentation I've found says that the GrideView's PageCount property is generated from the total records in the DataSource. Is there really no way to set the PageCount based on something else other than returning all of the records?
There could be tens of thousands of records in my data source so I'd rather not select all of them just to make the GridView's page count work. I probably could just ignore the GridView's page count and calculate it on my own, but if the framework has a way to do this, I'd rather use it.
I strongly recommend that you go the ObjectDataSource route.
If you are unfamiliar with this approach here are the basics:
1) Instead of manually setting the grid.DataSource property in the code behind, you add an extra element to the page . You set the DataSourceID of the grid to the id of your ObjectDataSource.
2) This is where you get real control. You create a new class and give it two functions "SelectRows()" and "GetCount()". You can put your logic in both functions and optimize to your heart's content. Feel free to use web services if that's what you need to work with, but under this method, you can call one to return rows and other to return the count.
3) use the ObjectDataSource's property editor to connect it to your class and enable paging. You're all set!
I strongly suggest you check out The Code Project's Example of using ObjectDataSource and GridView as this is clearly the intended way to support what you want.
Good luck!
You have to set AllowCustomPaging="true". And when databinding do:
mygrid.VirtualItemCount = totalRecords;
mygrid.DataSource = mysource;
mygrid.DataBind();
Update 1: Above is for datagrid only. Unfortunately the only supported way to do server side paging with the gridview is implementing an object datasource or implementing a custom datasource. Here is the related msdn doc http://msdn.microsoft.com/en-us/library/5aw1xfh3.aspx.
Update 2: This method now works with GridView as of .Net 4.5

Categories