views:

422

answers:

2

hi all, 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

+4  A: 

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.

Henk Holterman
really..sorry..i am a rookie....i try first..will feedback to you how i go
shrimpy
to be more precise it accepts any IEnumerable. Depending on what it detects (it does a little type sniffing it enables/disables functionality). For an IEnumerable it will not do paging because it wont now the total number of elements etc.
AZ
5 up votes? This answer doesn't address paging at all
Rafe Lavelle
+4  A: 

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.

Rafe Lavelle