使用 List() 时网格视图绑定问题
.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>
.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
变量是全局声明的。现在,当我运行此代码时,它显示以下错误:
数据源不支持服务器端数据分页
如果我使用 var 类型的变量那么它可以工作但是 我们不能全局声明var类型的变量,所以如果我使用它,那么我每次分页都必须调用这个方法。我不想使用Objectdatasource
。
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
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须使用
ToList()
方法将列表返回到gvFirst
。因此,请尝试以下操作:解释:
因为在
gvFirst
上,您设置了属性AllowPaging="true"
,因此分页可以与任何数据源一起使用实现ICollection
接口的对象。ProInfo.Take(PageSize)
返回一个IEnumerable
,这就是您需要调用ToList()
方法的原因。You must return a List to the
gvFirst
usingToList()
method. So try this:Explanation:
Because on the
gvFirst
you set the propertyAllowPaging="true"
than the paging can be used with any data source object that implementsICollection
interface.ProInfo.Take(PageSize)
returns anIEnumerable
and this is why you need to call theToList()
method.如果您将 Proinfo 全局声明为 ICollection
然后您使用 .Take 方法,您将得到结果作为不支持分页的可枚举,因此您必须将其转换为从 ICollection 继承的 List。
If you globaly declare Proinfo as a ICollection
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.