2009-11-02 16 views
50

Ho un GridView sullo schermo e ne ho bisogno per consentire il paging.L'origine dati non supporta l'impaginazione dei dati lato server

Markup:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
    AutoGenerateColumns="False" DataSourceID="ObjectDataSource1"> 
    <Columns> 
    <asp:BoundField DataField="appID" HeaderText="appID" SortExpression="appID" /> 
    </Columns> 
</asp:GridView> 

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
    SelectMethod="GetBookingId" 
    TypeName="AppointmentRepository"> 
    <SelectParameters> 
    <asp:Parameter Name="maximumRows" Type="Int32" /> 
    <asp:Parameter Name="startRowIndex" Type="Int32" /> 
    </SelectParameters> 
</asp:ObjectDataSource> 

Codice-behind:

ObjectDataSource1.SelectParameters["maximumRows"].DefaultValue = "10"; 
ObjectDataSource1.SelectParameters["startRowIndex"].DefaultValue = "0"; 

query LINQ:

public IQueryable<tblAppointment> GetBookingId(int maximumRows, int startRowIndex) 
{ 
    var result = (FROM a IN dc.tblAppointments 
        SELECT a).Skip(startRowIndex).Take(maximumRows); 
} 

Tuttavia ricevo questo errore:

The data source does not support server-side data paging.

Cosa sto sbagliando?

risposta

121

Un semplice ToList() sul risultato che var dovrebbe funzionare.

Edit: Come BornToCode spiegato nei commenti qui sotto la mia risposta, la ragione per l'errore è che l'origine dei dati dovrebbe attuare ICollection. IEnumerable no, quando si esegue ToList() lo converte in un elenco che implementa ICollection.

3

È anche possibile utilizzare il generico List<T>. Vedere il codice di esempio frammento di:

public List<Company> GetContactList(int startindex) 
{ 

    string path = Server.MapPath("~/contacts.xml"); 
    XDocument xd = XDocument.Load(path); 
    IEnumerable<Company> results = (from items in xd.Elements("Company").Elements("Contact") 
        select new Company 
        { 
         Id = items.Element("ID").Value, 
         Photo = (string)items.Element("photo").Value, 
         Name = (string)items.Element("Name").Value, 
         BloodGroup = (string)items.Element("Bg").Value, 
         Dob = (string)items.Element("dob").Value, 
         Anniversery = (string)items.Element("avd").Value, 
         Mobile = (string)items.Element("cnum").Value, 
         designation = (string)items.Element("desig").Value, 
         Team = (string)items.Element("team").Value 
        }).Skip(startindex*10).Take(10); 
    return (List<Company>) results; 
} 

È inoltre possibile utilizzare DataSet/DataTable invece di DataReader.

0

In ObjectDataSource è sufficiente aggiungere enablePaging="true" che funzionerà.

1

ho cambiato il mio codice a questo:

public List<string> ListofNewsTitle() 
{ 
    var query = from n in db.NewsEvents 
       orderby n.NewsDate descending 
       select n.NewsTitle; 
    return query.ToList();   
} 
1

.ToList() alla fine del DataSource, Sto assegnando lavorato per me come di seguito:

gvCaseLabelsLeft.DataSource = caseLabelsList.OrderBy(c=>c.caseLabelNumber).ToList(); 
0

query LINQ:

ProductController.cs:

List<Product> products= productModel.GetProducts(start, offset); 

ProductModel.cs:

public List<Product> GetProducts(int start, int offset) 
{ 
    IEnumerable<Product> query = from m in db.Products 
           orderby m.Id descending 
           select m; 
    query = query.Skip(start).Take(offset); 
    return query.ToList(); 
} 
0

se si utilizza SqlDataReader allora non sostenere Paging.

Soluzione a questo errore sta facendo uso di origini dati come raccolte di elenchi generici, DataTable, DataSet, ecc. Per associare il GridView.

Contrassegna la mia risposta se è stato risolto il problema.

Problemi correlati