使用ObjectDataSource错误消息对GridView进行排序

时间:2014-01-08 20:32:47

标签: c# asp.net sorting gridview objectdatasource

我正在按照教程在GridView中启用排序,该GridView具有objectdatasource作为数据源。 http://www.devtoolshed.com/content/gridview-objectdatasource-linq-paging-and-sorting

看起来非常简单,但是当我运行代码时,

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();我收到错误消息:限制必须具有非负值。 参数名称:限制 我不知道是什么导致了这个错误,我无法在任何地方找到任何相关信息。

这是我的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();
        }
    }

1 个答案:

答案 0 :(得分:0)

您的设置中至少缺少一些元素。

首先,从SelectParameters中删除ObjectDataSource。 然后确保在网格中设置PageSize。 最后,将SelectCountMethod = "SelectCount"添加到ObjectDataSource

如果您需要一个简明的示例来说明需要哪些属性才能获得正确的结果,请参考我多年前为学生提供的一个示例:

http://www.ii.uni.wroc.pl/~wzychla/ra2829/example3a.zip

有两个页面,Default.aspx显示如何将ObjectDataSourceGridViewDetailsView配对,Default2.aspx显示如何配对ObjectDataSourceListView