我正在学习一个教程,以在GridView中启用排序,该GridView具有objectdatasource作为数据源。 http://www.devtoolshed.com/content/gridview-objectdatasource-linq-paging-and-sorting使用ObjectDataSource对GridView进行排序错误消息
看来非常简单的,但是当我运行的代码,
public List<tbl_Batch> SelectAllList(string sSortType, int iBeginRowIndex, int iMaximumRows)
{
using (TestEntities dbContext = new TestEntities())
{
var query = from q in dbContext.tbl_Batch
select q; // sort
query = SelectAllSort(query, sSortType); // filter the list if needed
query = SelectAllQuery(query); // paginate
query = query.Skip(iBeginRowIndex).Take(iMaximumRows); // execute the query and convert to list
return query.ToList();
}
}
当它到达最后一行返回query.ToList();我收到错误消息:限制必须有一个非负值。 参数名称:limit 我不知道是什么原因导致了这个错误,我无法在任何地方找到任何有关它的信息。
这是我的GridView的代码,任何人都可以告诉我我做错了什么?
<asp:GridView ID="GridView1" runat="server" DataKeyNames="intBatchID"
AllowPaging="True" OnDataBound="GridView1_DataBound" OnRowDataBound="GridView1_RowDataBound"
AllowSorting="True" AutoGenerateColumns="False" SkinID="NOCTS" EnableSortingAndPagingCallbacks="True"
BorderStyle="Solid" HeaderStyle-BackColor="#990033" Width="1000px"
DataSourceID="ObjectDataSource1" OnSorting="GridView1_Sorting">
<HeaderStyle ForeColor="White"></HeaderStyle>
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="intBatchID" HeaderText="Batch ID" DataNavigateUrlFormatString="TestPage1.aspx?intBatchID={0}" DataTextField="intBatchID" />
<asp:BoundField DataField="vcharName" HeaderText="Name" ReadOnly="True"
SortExpression="vcharName" />
<asp:BoundField DataField="dtmScheduled" HeaderText="Date Scheduled"
ReadOnly="True" SortExpression="dtmScheduled" />
<asp:BoundField DataField="intBatchPriorityLevel"
HeaderText="Priority Level" ReadOnly="True"
SortExpression="intBatchPriorityLevel" />
</Columns>
<PagerSettings Mode="NumericFirstLast" Position="TopAndBottom" PageButtonCount="4" PreviousPageText="Previous" NextPageText="Next" FirstPageText="First" LastPageText="Last" />
<PagerStyle HorizontalAlign="Center" />
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="SelectAllList" TypeName="NOCTSWebApplication.App_Code.Class3"
OnSelected="ObjectDataSource1_Selected" EnablePaging="True"
MaximumRowsParameterName="iMaximumRows" OldValuesParameterFormatString="original_{0}"
StartRowIndexParameterName="iBeginRowIndex" SortParameterName="sSortType">
<SelectParameters>
<asp:Parameter Name="sSortType" Type="String" />
<asp:Parameter Name="iBeginRowIndex" Type="Int32" />
<asp:Parameter Name="iMaximumRows" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
如果您需要我的代码,请告诉我。 这里是我的代码的其余部分:
private IQueryable<tbl_Batch> SelectAllQuery(IQueryable<tbl_Batch> query)
{
return query;
}
private IQueryable<tbl_Batch> SelectAllSort(IQueryable<tbl_Batch> query, string sSortType)
{
using (TestEntities dbContext = new TestEntities())
{
bool bIsSortDescending = false;
if (!String.IsNullOrEmpty(sSortType))
{
string[] sValues = sSortType.Split(' ');
if (sValues.Length > 1)
{
if (sValues[1].ToUpper() == "DESC")
{
bIsSortDescending = true;
}
}
}
if (!String.IsNullOrEmpty(sSortType))
{
query = dbContext.tbl_Batch.OrderBy(sSortType);
}
else
{ // use a default sort here
if (bIsSortDescending)
{
query = query.OrderByDescending(q => q.intBatchID);
}
else
{
query = query.OrderBy(q => q.vcharName);
}
}
return query;
}
}
public int SelectCount()
{
using (TestEntities dbContext = new TestEntities())
{
var query = from q in dbContext.tbl_Batch
select q;
query = SelectAllQuery(query); // execute the query and return the count
return query.Count();
}
}
'SelectAllQuery' - 它是什么?还要检查分页限制值(iMaximumRows)。 –
@WiktorZychla iMaxiumumRows的值是-1。我添加了其余的代码,以便可以看到SelectAllQuery。谢谢你的帮助。 – hollyquinn
您不能从结果集中取-1行。这就是为什么你会得到一个例外。 –