Bind GridView with many records

前端 未结 1 620
醉话见心
醉话见心 2021-01-16 11:09

I have a GridView which shows only 50 records at a time through paging. If i bind that to a source say containing 150 records, it works like charm but when the record size i

相关标签:
1条回答
  • 2021-01-16 11:46

    Here is, accoring to your change, how I would do that :

    public static Customers GetAllCustomer(int startIndex, int nbrOfResults, out total) {
       Customers customers = new Customers();
    
       NShop_SmallEntities data = new NShop_SmallEntities();
       var dbCustomers = from c in data.Customers
                         select c;
    
       // Retreiving total number of customers. NEEDED to get 
       // ObjectDataSource working.
       total = dbCustomers.Count();
    
       foreach (var dbCustomer in dbCustomers
                             .Skip((startIndex * nbrOfResults) + 1)
                             .Take(NumberOfResults).ToList() {
          customers.Add(Customer.GetCustomer(dbCustomer));
       }
       return customers;
    }
    
    /// <summary>
    /// This methods must have the same signature than the "real" one... exept the name :oP
    /// </summary>
    public static int GetAllCustomerCount(int startIndex, int nbrOfResults, out total) {
          return (from c in data.Customers select c).Count();
    }
    

    And now in your page ;

    <asp:GridView ID="gvBulletins" runat="server" AllowPaging="True" 
       ObjectDataSourceID="objCustomersDS">
    </asp:GridView>
    
    <asp:ObjectDataSource ID="objCustomersDS" runat="server" 
         SelectMethod="GetAllCustomer" SelectCountMethod="GetAllCustomerCount" 
         TypeName="AssemblyName" EnablePaging="True" MaximumRowsParameterName="nbrOfResults"
         StartRowIndexParameterName="startIndex">
        <SelectParameters>
             <asp:Parameter Name="startIndex" Type="Int32" />
             <asp:Parameter Name="nbrOfResults" Type="Int32" />
             <asp:Parameter Direction="Output" Name="total" Type="Int32" />
        </SelectParameters>
    </asp:ObjectDataSource>
    
    0 讨论(0)
提交回复
热议问题