C# GridView Paging - object data source

4/1/2014·5 min read

How to use ObjectDataSource + GridView to enble paging?

If you'd like to do following kind of paging, this's article'll demostrate how to do that.

 

Your Data looks like

data object

want to display following data to UI by using GridView

 protected  List <ProductTest > GetData()
 		{
 			return  new  List <ProductTest >() 
          {
                new  ProductTest { Id=1, DescTest="Google TVGetting your email to look great in all the major email clients can be a challenge at times, " , NameTest="Google TV" , PriceTest=11},
 					new  ProductTest { Id=2, DescTest="Survivalists are the prosumer reviewers of pouches and bags" , NameTest="Google TV" , PriceTest=11},
 					new  ProductTest { Id=3, DescTest="Why do Olympic records keep getting broken?" , NameTest="Google TV" , PriceTest=11},
 					new  ProductTest { Id=4, DescTest="How does the brain think?" , NameTest="Google TV" , PriceTest=11},
 					new  ProductTest { Id=5, DescTest="Real history from a pretend pirate" , NameTest="Google TV" , PriceTest=11},
 					new  ProductTest { Id=6, DescTest="Reggie Watts making fun of \\"big idea\\" presentations" , NameTest="Google TV" , PriceTest=11},
 					   new  ProductTest { Id=7, DescTest="Google TVGetting your email to look great in all the major email clients can be a challenge at times, " , NameTest="Google TV" , PriceTest=11},
 					new  ProductTest { Id=8, DescTest="Survivalists are the prosumer reviewers of pouches and bags" , NameTest="Google TV" , PriceTest=11},
 					new  ProductTest { Id=9, DescTest="Why do Olympic records keep getting broken?" , NameTest="Google TV" , PriceTest=11},
 					new  ProductTest { Id=10, DescTest="How does the brain think?" , NameTest="Google TV" , PriceTest=11},
 					new  ProductTest { Id=11, DescTest="Real history from a pretend pirate" , NameTest="Google TV" , PriceTest=11},
 					new  ProductTest { Id=12, DescTest="Reggie Watts making fun of \\"big idea\\" presentations" , NameTest="Google TV" , PriceTest=11},
 					   new  ProductTest { Id=13, DescTest="Google TVGetting your email to look great in all the major email clients can be a challenge at times, " , NameTest="Google TV" , PriceTest=11},
 					new  ProductTest { Id=14, DescTest="Survivalists are the prosumer reviewers of pouches and bags" , NameTest="Google TV" , PriceTest=11},
 					new  ProductTest { Id=15, DescTest="Why do Olympic records keep getting broken?" , NameTest="Google TV" , PriceTest=11},
 					new  ProductTest { Id=16, DescTest="How does the brain think?" , NameTest="Google TV" , PriceTest=11},
 					new  ProductTest { Id=17, DescTest="Real history from a pretend pirate" , NameTest="Google TV" , PriceTest=11},
 					new  ProductTest { Id=18, DescTest="Reggie Watts making fun of \\"big idea\\" presentations" , NameTest="Google TV" , PriceTest=11},
 					   new  ProductTest { Id=19, DescTest="Google TVGetting your email to look great in all the major email clients can be a challenge at times, " , NameTest="Google TV" , PriceTest=11},
 					new  ProductTest { Id=20, DescTest="Survivalists are the prosumer reviewers of pouches and bags" , NameTest="Google TV" , PriceTest=11},
 					new  ProductTest { Id=21, DescTest="Why do Olympic records keep getting broken?" , NameTest="Google TV" , PriceTest=11},
 					new  ProductTest { Id=22, DescTest="How does the brain think?" , NameTest="Google TV" , PriceTest=11},
 					new  ProductTest { Id=23, DescTest="Real history from a pretend pirate" , NameTest="Google TV" , PriceTest=11},
 					new  ProductTest { Id=24, DescTest="Reggie Watts making fun of \\"big idea\\" presentations" , NameTest="Google TV" , PriceTest=11},
 
          };
 		}
 
C# server side looks
    public  partial  class  GridView_Paging_Object  : System.Web.UI.Page 
     {
         protected  void  Page_Load(object  sender, EventArgs  e)
         {
 
         }
     }
 
aspx page's sample code
 <asp:GridView  ID="GridView1"  runat="server"  AllowPaging="True"
         AutoGenerateColumns="False"  DataSourceID="ObjectDataSource1"  PageSize="5">
         <Columns>
             <asp:BoundField  DataField="Id"  HeaderText="Id"  SortExpression="Id"  /> 
             <asp:BoundField  DataField="NameTest"  HeaderText="NameTest"
                 SortExpression="NameTest"  /> 
             <asp:BoundField  DataField="DescTest"  HeaderText="DescTest"
                 SortExpression="DescTest"  /> 
             <asp:BoundField  DataField="PriceTest"  HeaderText="PriceTest"
                 SortExpression="PriceTest"  /> 
         </Columns>
     </asp:GridView>
     <asp:ObjectDataSource  ID="ObjectDataSource1"  runat="server"
         SelectMethod="GetProductData"  TypeName="AspnetGridView.App_Data.BusinessLogic">
     </asp:ObjectDataSource>
 

How Html looks like

 

the html table just fetch the page need to display to UI, so everytime, when click paging index, looks like it go back to server to get data.

 

Using this Object Data Source is not bad idea, if you UI don't need to control the GridView's datasource, like you don't need to add or edit value when GridView's paging event. If its simple like this example, you could using object data source