数据源不支持服务器端数据分页。

时间:2021-12-19 16:18:09

I have a GridView on my screen and need it to allow paging.

我的屏幕上有一个GridView,需要它允许分页。

Markup:

标记:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
  AutoGenerateColumns="False" DataSourceID="ObjectDataSource1">
  <Columns>
    <asp:BoundField DataField="appID" HeaderText="appID" SortExpression="appID" />
  </Columns>
</asp:GridView>

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
  SelectMethod="GetBookingId" 
  TypeName="AppointmentRepository">
  <SelectParameters>
    <asp:Parameter Name="maximumRows" Type="Int32" />
    <asp:Parameter Name="startRowIndex" Type="Int32" />
  </SelectParameters>
</asp:ObjectDataSource>

Code-behind:

后台代码:

ObjectDataSource1.SelectParameters["maximumRows"].DefaultValue = "10";
ObjectDataSource1.SelectParameters["startRowIndex"].DefaultValue = "0";

LINQ query:

LINQ查询:

public IQueryable<tblAppointment> GetBookingId(int maximumRows, int startRowIndex)
{
    var result = (FROM a IN dc.tblAppointments
                  SELECT a).Skip(startRowIndex).Take(maximumRows);
}

However I receive this error:

但是我收到这个错误:

The data source does not support server-side data paging.

数据源不支持服务器端数据分页。

What am I doing wrong?

我做错了什么?

7 个解决方案

#1


123  

A simple ToList() on your result var should work.

结果var上的简单ToList()应该可以工作。

Edit: As BornToCode explained in comments below my answer, the reason for the error is that the data source should implement ICollection. IEnumerable does not, when you do ToList() it converts it into a list which implements ICollection.

编辑:正如BornToCode在我的回答下面的评论中所解释的,错误的原因是数据源应该实现ICollection。当您执行ToList()时,IEnumerable不会将其转换为实现ICollection的列表。

#2


3  

You can use generic List<T> also. See the sample code snippet:

您也可以使用泛型列表 。参见示例代码片段:

public List<Company> GetContactList(int startindex)
{

    string path = Server.MapPath("~/contacts.xml");
    XDocument xd = XDocument.Load(path);
    IEnumerable<Company> results = (from items in xd.Elements("Company").Elements("Contact")
                   select new Company
                   {
                       Id = items.Element("ID").Value,
                       Photo = (string)items.Element("photo").Value,
                       Name = (string)items.Element("Name").Value,
                       BloodGroup = (string)items.Element("Bg").Value,
                       Dob = (string)items.Element("dob").Value,
                       Anniversery = (string)items.Element("avd").Value,
                       Mobile = (string)items.Element("cnum").Value,
                       designation = (string)items.Element("desig").Value,
                       Team = (string)items.Element("team").Value
                   }).Skip(startindex*10).Take(10);
    return (List<Company>) results;
}

You can also use DataSet/DataTable instead of DataReader.

您还可以使用数据集/DataTable而不是DataReader。

#3


1  

I've changed my code to this:

我把我的代码改为:

public List<string> ListofNewsTitle()
{
    var query = from n in db.NewsEvents
                orderby n.NewsDate descending
                select n.NewsTitle;
    return query.ToList();        
}

#4


1  

.ToList() at the end of the DataSource, I am assigning worked for me like below:

.ToList()在数据源结束时,我为自己分配工作如下:

gvCaseLabelsLeft.DataSource = caseLabelsList.OrderBy(c=>c.caseLabelNumber).ToList();

#5


0  

In ObjectDataSource just add enablePaging="true" that will work.

在ObjectDataSource中,只添加enable分页=“true”即可工作。

#6


0  

LINQ query:

LINQ查询:

ProductController.cs:

ProductController.cs:

List<Product> products= productModel.GetProducts(start, offset);

ProductModel.cs:

ProductModel.cs:

public List<Product> GetProducts(int start, int offset)
{
    IEnumerable<Product> query = from m in db.Products
                                 orderby m.Id descending
                                 select m;
    query = query.Skip(start).Take(offset);
    return query.ToList();
}

#7


0  

If You are Using SqldataReader then its not support Paging.

如果您正在使用SqldataReader,那么它不支持分页。

Solution to this error is making use of DataSources such as Generic List collections, DataTables, DataSets, etc. to bind the GridView.

解决此错误的方法是使用诸如泛型列表集合、DataTables、DataSets等数据源来绑定GridView。

#1


123  

A simple ToList() on your result var should work.

结果var上的简单ToList()应该可以工作。

Edit: As BornToCode explained in comments below my answer, the reason for the error is that the data source should implement ICollection. IEnumerable does not, when you do ToList() it converts it into a list which implements ICollection.

编辑:正如BornToCode在我的回答下面的评论中所解释的,错误的原因是数据源应该实现ICollection。当您执行ToList()时,IEnumerable不会将其转换为实现ICollection的列表。

#2


3  

You can use generic List<T> also. See the sample code snippet:

您也可以使用泛型列表 。参见示例代码片段:

public List<Company> GetContactList(int startindex)
{

    string path = Server.MapPath("~/contacts.xml");
    XDocument xd = XDocument.Load(path);
    IEnumerable<Company> results = (from items in xd.Elements("Company").Elements("Contact")
                   select new Company
                   {
                       Id = items.Element("ID").Value,
                       Photo = (string)items.Element("photo").Value,
                       Name = (string)items.Element("Name").Value,
                       BloodGroup = (string)items.Element("Bg").Value,
                       Dob = (string)items.Element("dob").Value,
                       Anniversery = (string)items.Element("avd").Value,
                       Mobile = (string)items.Element("cnum").Value,
                       designation = (string)items.Element("desig").Value,
                       Team = (string)items.Element("team").Value
                   }).Skip(startindex*10).Take(10);
    return (List<Company>) results;
}

You can also use DataSet/DataTable instead of DataReader.

您还可以使用数据集/DataTable而不是DataReader。

#3


1  

I've changed my code to this:

我把我的代码改为:

public List<string> ListofNewsTitle()
{
    var query = from n in db.NewsEvents
                orderby n.NewsDate descending
                select n.NewsTitle;
    return query.ToList();        
}

#4


1  

.ToList() at the end of the DataSource, I am assigning worked for me like below:

.ToList()在数据源结束时,我为自己分配工作如下:

gvCaseLabelsLeft.DataSource = caseLabelsList.OrderBy(c=>c.caseLabelNumber).ToList();

#5


0  

In ObjectDataSource just add enablePaging="true" that will work.

在ObjectDataSource中,只添加enable分页=“true”即可工作。

#6


0  

LINQ query:

LINQ查询:

ProductController.cs:

ProductController.cs:

List<Product> products= productModel.GetProducts(start, offset);

ProductModel.cs:

ProductModel.cs:

public List<Product> GetProducts(int start, int offset)
{
    IEnumerable<Product> query = from m in db.Products
                                 orderby m.Id descending
                                 select m;
    query = query.Skip(start).Take(offset);
    return query.ToList();
}

#7


0  

If You are Using SqldataReader then its not support Paging.

如果您正在使用SqldataReader,那么它不支持分页。

Solution to this error is making use of DataSources such as Generic List collections, DataTables, DataSets, etc. to bind the GridView.

解决此错误的方法是使用诸如泛型列表集合、DataTables、DataSets等数据源来绑定GridView。