ASP.NET DataGridView paging with server-sided paging - asp.net

I am passing the PageNumber and PageSize to a stored procedure, which returns only that page of data. It also returns a record count what the total number of records would be if I returned all of them at once.
Generally, how do i hook this up to the DataGridView to enable paging?
It seems like the expectation is for the resultset to contain the complete dataset.
Many of the properties that I expect to be able to set, like RecordCount, appear to be read only.
Could someone give me general pointers?

I created a similar solution once, and adjusted a GridView (not sure if you mean a DataGrid or a GridView) to use server-side paging. The code is too much to post here and since there's no option for attachments here, you can download the code from http://www.raskenlund.com/downloads/GridView.zip

Check out the following article which describes the same thing:
http://www.highoncoding.com/Articles/210_GridView_Custom_Paging.aspx

Related

How to show a row from DB and update it value with gridview

My goal is to load a row from db to a webform and let the user update it's value .
The user searches an id (i have a stored procedure for that), how i show that data from the
row nicely in the web page through the dal layer. After the data is shown on the page the user need to update a cell in the row and send it.(the part of updating is not the problem).
in other questions how should the dal method should look and how i integrate it's result in the presentation layer (the aspx webform).
thanks a lot.
p.s.
I've done a little reading but i don't know what exactly to use data object, data row , data table, object data source. i'm little confused by the data bind alternatives.
You have several options and one of them is using ObjectDataSource.
If it is going to be a single record you can use DetailsView or Formview else you use GridView.
Check the tutorials here:
Displaying Data with ObjectDataSource
Data Access Tutorials

gridview asp.net

I have a gridview which was working fine with a small dataset in development. In production it has to bind to thousands of records, which is making it slower to load. Is there a way to improve performance, like retrieving the data during gridview pageindex changing?
Also chances are you only want to bind it once. So you should (if not already):
if(!IsPostback)
{
DatabindGridLogicHere();
}
This way your GridView will only have to hit the db the first time to get the data.
First and formost turn off ViewState.
You should tell your datasource to take less records and then enable paging in your grid and datasource.
You can enable "AllowPaging" property to true in your GridView and give a page size say 10. Then Write your data retrieval logic to return a batch of data instead of the whole set of data at once.
When you write the SQL query make sure to order it by an ID.
Thus if the page index is 1 you can take the first batch of data by passing page index of 1 and the page size of 10.
Logic will be;
SELECT [RequiredFields]
FROM [YourDataSource]
WHERE (Id>=((PageIndex-1)*pageSize) AND Id<(PageSize*pageIndex)) ORDER BY Id
In the first page it will return first set of entries of those Ids starting from 0 to 9. In the second page it returns entries of those Ids starting from 10 to 19 assuming the pageSize is 10. You can change the page size and the query logic as you wish.
But if sorting is enable then this will not produce accurate results.

Easiest method to build where clause from checked items in DataList of PreviousPage

I have a selection list that is generated dynamically, it lists a number of checkboxes that are based on an end-user editable table, as such I have no idea what data, or how much, might be contained in this table and how many checkboxes or what they might contain, other than the primary keys of the table.
The user will select the checks they wish to see, and then control is passed to another page via PostBackUrl. The second page has to figure out which records to show (build it's where clause) based on the checkboxes checked in the previous page.
So, my problem is several-fold. First, asp:CheckBoxes don't have values. This can be worked around by a number of methods. Right now, i'm using a placeholder and dynamically creating the checkboxes in the ItemDataBound event of the DataList. I set the ID to "CheckboxKey1Key2" (where Key1 and Key2 are the primary keys of the check items).
Second, I have to walk through the controls of the PreviousPage to dig out all these values. That in itself is also a pain, but doable.
Now, my thinking is to build the where clause of my Linq2Sql query based on the keys I got from decoding the checked checkbox names. This all seems like a lot of jumping through hoops for something that shouldn't be this difficult. Am I missing something? Does anyone have any better solutions?
Make a class with a structure for your values that will be useful and easy for you to use on the result page. Then when the user clicks whatever it is they click to go to the result page, loop through the DataList, create a collection from the checked items, and you will only need to grab one object instead of everything, when you need to format your query.
Then when you get to the result page, loop through the collection and build the query in the loop. Pretty easy and not much code.

Gridview Sorted Event

I've very small question that drives me mad :)
I've a Gridview (bind from db nothing special there) and I use small function that runs on the griviewrows and sets .Visable to false in case they don't match search criterias. It works fine but when I try to sort the grid view (by clicking on the header) all the "hidden" rows shows up again.
I tried to use the "GridView_Sorted" event in order to run on the gridview and hide again but it doesn't seem to do anything. The select statement is stored procedure so I can't use filtering expressions.
My Question is - Is there a way to run the hiding function after the sort
(as "Occurs when the hyperlink to sort a column is clicked, but after the GridView control handles the sort operation." {http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sorted.aspx} suggests )
The GridView's PreRender event should do the trick.
You could just walk GridView.Rows and apply your logic there... That way it's guaranteed to occur at the right time wether the sort happens or not.
How are you binding the data?
Maybe it would help only to bind the used data (rows) to the grid, because binding not displayed data is kind of an overhead.

asp.net custom datapager

in all the datapager examples i've seen (using a LinqDataSource tied to a ListView) the queries to the database return the full recordset. how can i get JUST the page of rows that i want to display?
for example, if i have a table with 1million rows and i want to see the 10 results on page 5, i would be getting rows 51 to 60.
i'm sure i have to implement custom paging but i haven't found any good examples.
There are many ways of doing this, however, I personally like a SQL based solution that goes to the database and gets the result set. This 4GuysFromRolla Article does a good job of explaining it.
If you're using MSSql2005, take a look at this article.
As you can see, the trick is to use the function ROW_NUMBER(), that allow you to get the sequential number of a row in a recordset. With it you can simply enable pagination based upon the number of rows you want to get in a page.
I was under the impression (from Scott Guthie's blog post "LINQ to SQL (Part 9)") that the LinqDataSource handles the paging for you at the database level:
One of the really cool things to notice above is that paging and sorting still work with our GridView - even though we are using a custom Selecting event to retrieve the data. This paging and sorting logic happens in the database - which means we are only pulling back the 10 products from the database that we need to display for the current page index in the GridView (making it super efficient).
(original emphasis)
If you are using some custom paging, you can do something like this in LINQ to SQL:
var tagIds = (from t in Tags where tagList.Contains(t.TagText) select t.TagID).Skip(10).Take(10).ToList();
This is telling LINQ to take 10 rows (equivalent to T-SQL "TOP 10"), after skipping the first 10 rows - obviously these values can be dynamic, based on the Page Size and page number, but you get the idea.
The referenced post also talks about using a custom expression with a LinqDataSource.
Scott has more information on Skip/Take in Part 3 as well.

Resources