Question

i want to ask a question about how to do paging in ASP.net coding with C#.

the thing i try to do is that, i want to display some content, have multi column and multi rows.

in the actually coding, i have a list of object, let say Object A

class A {
   integer id;
   string name;
   string desc;
}

and want to display this onto the page with pagable functionality.

i do google search, ASP.net offer gridview and listview, but they all require a datasource, which mean that table directly bind to the database.

that is not what i want, coz my list of object A come from some where else, not from my database (e.g it is a composite set of data, which generate in runtime)

so it there anyway i still can use those benifit component, or i have to do it all on my own for the paging???

thx

Was it helpful?

Solution

A DataSource property will also accept a List<> or BindingList<> .

To use this in code:

protected void Page_Load(object sender, EventArgs e)
{
    var data = new List<Sample>();
    data.Add (...);

    GridView1.DataSource = data;
    GridView1.DataBind();
}

And maybe some IsPostback logic etc.

OTHER TIPS

My advice is to use a GridView, with which you can use an ObjectDataSource, which can take its underlying data from a class method that you specify. So the class method might be (following your code example):

public static List<A> GetAllAs()
{
    return myAs;
}

and your aspx page would contain

<asp:ObjectDataSource ID="MyODS" runat="server" TypeName="Namespace.Classname" SelectMethod="GetAllAs" />

<asp:GridView ID="grdMyGridView" runat="server" DataSourceID="MyODS" AllowPaging="True" ... >

The TypeName and SelectMethod attributes of the ObjectDataSource tell it which method, and where, to use to fill the ODS with data. The AllowPaging="True" gives you paging on the GridView automatically.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top