jats
jats

Reputation: 137

Grid view Bind Problem when using List()

The .aspx:

 <asp:GridView ID="gvFirst" runat="server" AutoGenerateColumns="false" 
        AllowPaging="true" 
        ondatabound="gvFirst_DataBound" >
        <Columns>
            <asp:BoundField DataField="ID" HeaderText="ProductID"/>
            <asp:BoundField DataField="Name" HeaderText="ProductName" />
        </Columns>
        <PagerTemplate>
            <asp:Panel ID="pnlPager" runat="server">
            </asp:Panel>
        </PagerTemplate>
    </asp:GridView>

The .cs:

 public class Productinformation
    {
     public int PID
     {
         get;
         set;
     }
     public string PName
     {
         get;
         set;
     }
    }
       using (NorthWindDataContext _NorthWindDataContext = new NorthWindDataContext())
        {
            Proinfo = new List<Productinformation>();
            Proinfo = (from p in _NorthWindDataContext.Products
                       select new Productinformation
                       {
                           PID = p.ProductID,
                           PName = p.ProductName,
                       }).ToList();

            gvFirst.DataSource =  Proinfo.Take(PageSize) ;
            gvFirst.DataBind();
        }

Proinfo variable is declared globally. Now When i run this code, it shows me the following error :

the data source does not support server-side data paging

If i use var type of variable then it is worked but we can't declare var type of variable globally,so if i used it, then i have to call this method every time in paging. and i don't want to use Objectdatasource.

Upvotes: 0

Views: 565

Answers (2)

Arabela Paslaru
Arabela Paslaru

Reputation: 7074

You must return a List to the gvFirst using ToList() method. So try this:

gvFirst.DataSource =  Proinfo.Take(PageSize).ToList();

Explanation:

Because on the gvFirst you set the property AllowPaging="true" than the paging can be used with any data source object that implements ICollection interface.

ProInfo.Take(PageSize) returns an IEnumerable and this is why you need to call the ToList() method.

Upvotes: 1

Mattias Josefsson
Mattias Josefsson

Reputation: 1197

If you globaly declare Proinfo as a ICollection

private ICollection<Productinformation> Proinfo = null

Then then you use the .Take method you get the result as a enumerable that does not support paging so you have to convert it to a List that inherit from the ICollection.

gvFirst.DataSource =  Proinfo.Take(PageSize).ToList();

Upvotes: 1

Related Questions