GridView分页与对象数据源和Linq

时间:2014-03-03 17:32:17

标签: c# asp.net linq gridview

您好我正在按照以下教程中的说明尝试使用Linq和对象数据源向我的GridView添加分页。

http://www.codeproject.com/Articles/24480/Using-Linq-to-paginate-your-ObjectDataSource

我会在下面发布我的代码,并希望有人可以帮助我。

Gridview和ObjectDataSource

<asp:GridView ID="gvGeneric" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1" AllowPaging="True">
                <Columns>                                             
                    <asp:BoundField DataField="dtmDateCalled" HeaderText="dtmDateCalled" SortExpression="dtmDateCalled" />                                        
                    <asp:BoundField DataField="vcharDiscussion" HeaderText="vcharDiscussion" SortExpression="vcharDiscussion" />                       
                    <asp:TemplateField HeaderText="Staff">
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("OCASStaff.vcharStaffFirstName") %>'></asp:Label>&nbsp;<asp:Label ID="Label2" runat="server" Text='<%# Bind("OCASStaff.vcharStaffLastName") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>  
                </Columns>
            </asp:GridView>
            <br />
            <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="getPhoneLogs" TypeName="CTS.BLL.PhoneLogsBLL" OldValuesParameterFormatString="original_{0}">
                <SelectParameters>
                    <asp:SessionParameter DefaultValue="20" Name="ResultsPerPage" SessionField="ResultsPerPage"
                        Type="Int32" />
                    <asp:SessionParameter DefaultValue="1" Name="PageNumber" SessionField="PageNumber"
                        Type="Int32" />
                </SelectParameters>
            </asp:ObjectDataSource>

Linq方法

 public List<OCASPhoneCalls> getPhoneLogs(int ResultsPerPage, int PageNumber)
    {
        using (var repo = new OCASPhoneCallsRepository(new UnitOfWorkCTS()))
        {

            int skip = (PageNumber - 1) * ResultsPerPage;
            int take = ResultsPerPage;

            List<OCASPhoneCalls> phone = repo.AllIncluding(p => p.OCASStaff)
                .Where(y => y.intNIOSHClaimID == null)
                .OrderBy(o => o.sintStaffID)
                .Skip(skip).Take(ResultsPerPage)
                .ToList();
            return phone;
        }           
    }        

0 个答案:

没有答案