C# 数据源不支持服务器端数据分页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1661292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The data source does not support server-side data paging
提问by ClareBear
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?
我究竟做错了什么?
采纳答案by almog.ori
A simple ToList()
on your result var should work.
一个简单ToList()
的结果变量应该可以工作。
Edit:
As 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.
编辑:正如我的答案下面的评论中所解释的,错误的原因是数据源应该实现 ICollection。IEnumerable 不会,当您这样做时,ToList()
它会将其转换为实现 ICollection 的列表。
回答by renjucool
You can use generic List<T>
also. See the sample code snippet:
您也可以使用泛型List<T>
。请参阅示例代码片段:
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.
您还可以使用 DataSet/DataTable 代替 DataReader。
回答by Vporecha
In ObjectDataSource just add enablePaging="true"
that will work.
在 ObjectDataSource 中只需添加即可enablePaging="true"
。
回答by Data_Scientists
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();
}
回答by sakhya
.ToList()
at the end of the DataSource, I am assigning worked for me like below:
.ToList()
在数据源的末尾,我正在为我分配如下工作:
gvCaseLabelsLeft.DataSource = caseLabelsList.OrderBy(c=>c.caseLabelNumber).ToList();
回答by Shibu Thomas
LINQ query:
LINQ 查询:
ProductController.cs:
产品控制器.cs:
List<Product> products= productModel.GetProducts(start, offset);
ProductModel.cs:
产品型号.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();
}
回答by Akhil Singh
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.
解决此错误的方法是使用通用列表集合、数据表、数据集等数据源来绑定 GridView。
回答by Aljohn Yamaro
Try this article https://www.aspsnippets.com/Articles/ASPNet-GridView-The-data-source-does-not-support-server-side-data-paging.aspx
in summary try to use these lines
总之尝试使用这些行
private void BindGrid()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, ContactName, Country FROM Customers"))
{
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
GridView1.DataSource = sdr;
GridView1.DataBind();
}
con.Close();
}
}
}
protected void OnPaging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
this.BindGrid();
}
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true"
OnPageIndexChanging="OnPaging">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>