You can handle database paging through LINQ to SQL by using two methods
Skip()—Enables you to skip a certain number of records.Take()—Enables you to take a certain number of records.
Example is shown as below:
Add a Class file to project which would be added to APP_CODE folder.
For performing paging through LINQ to SQL, you would need to create a partial class Products having methods to select the records, record count and handle paging. This is shown below
Note: Please use required namespace as shown in picture for this code to work.
Public partial class Products{// Select method - selects all rows from product tablepublic static IEnumerableSelect() {MydbDataContext db1 = new MydbDataContext();return db1.Products;}//Select Paged Method - handles pagingpublic static IEnumerableSelectPaged(int startrowindex, int maximumrows) {return Select().Skip(startrowindex).Take(maximumrows);}//Count Method - return countpublic static int selectcount(){MydbDataContext db1 = new MydbDataContext();return db1.Products.Count();}}
Now add a gridview on defaulf.aspx. Allow paging and set page size = 5
Add an object data source. Set Enable paging = True, SelectMethod = SelectPaged and SelectCountMethod = selectcount, TypeName = Products. (You may perform this activity through wizard)
No comments:
Post a Comment