ASP.NET 4.5数据绑定模型SelectMethod抛出错误

时间:2014-02-14 10:40:53

标签: c# gridview model asp.net-4.5 selectmethod

我对这种新模式感到困惑。在阅读教程时,一切看起来都很简单。但是我无法完成的最简单的任务 - 将数据模型绑定到GridView。 这是GridView代码:

 <asp:GridView ID="gvIlves" runat="server" AllowPaging="True" AllowSorting="True" ItemType="GuideOnline_1_.Models.xTourist"
                AutoGenerateColumns="False" DataKeyNames="Kod" CssClass="table table-striped tablesorter"
                ClientIDMode="Static" PageSize="50" SelectMethod="SelectArrival" UpdateMethod="UpdateArrival">

SelectMethod:

public IQueryable<GuideOnline_1_.Models.xTourist> SelectArrival()
        {
            var now = DateTime.Today.AddDays(-3);
            IQueryable<GuideOnline_1_.Models.xTourist> arrivals = _db.xTourist;
            arrivals = arrivals.Where(p => p.Ответственный !=null).Where(p => p.Номер == null).Where(p => p.Датапр >= now);
            return arrivals;
        }

这看起来简单流畅,但我收到了错误: 当DataBoundControl启用分页时,SelectMethod应返回IQueryable或应具有所有这些必需参数:int startRowIndex,int maximumRows,out int totalRowCount

2 个答案:

答案 0 :(得分:2)

愚蠢的错误。我只是在ItemType中提到了错误的表名。

答案 1 :(得分:1)

这对我有用:

 <asp:GridView ID="gvIlves" runat="server" AllowPaging="True" AllowSorting="True" ItemType="WebApplication1.Tourist"
            AutoGenerateColumns="True" DataKeyNames="Kod" CssClass="table table-striped tablesorter"
            ClientIDMode="Static" PageSize="50" SelectMethod="SelectArrival"></asp:GridView>

代码:

namespace WebApplication1
{
public partial class _Default : Page
{
    public IQueryable<Tourist> SelectArrival()
    {
        return new EnumerableQuery<Tourist>(new List<Tourist>
        {
            new Tourist{ Kod = "1", Name = "Joe", Age = "35"},
            new Tourist{ Kod = "2", Name = "Cliff", Age = "45"},
            new Tourist{ Kod = "3", Name = "Dan", Age = "32"},
        });
    }
}

public class Tourist
{
    public string Kod { get; set; }
    public string Name { get; set; }
    public string Age { get; set; }
}
}