Binding a GridView to a custom object

G

Guest

Hello,

I am trying to bind a GridView to a custom object I have created. First,
here is what I'm trying to do:

I have a wizard for adding/editing Users. When the wizard begins, a User
object (custom class) is created, and properties are populated in each step
of the wizard. In one of the steps, the User is assigned to 1 or more
Programs. Assigned Programs are stored by the User object in a List, and
displayed in a GridView in the step. At the end of the wizard, the User is
saved to the database.

I'm pretty new to asp.net 2.0, but it seemed that an easy way to handle this
would be to bind the GridView to the User object via an ObjectDataSource.
Since I've stored the Programs in a List, it's easy to create a Select method
in the User class that returns this List to the ObjectDataSource. Methods to
insert, update, and delete are not too complicated either.

The problem is that it appears that the ObjectDataSource will create a new
User object each time it is called upon to select, insert, update, or delete
Program assignments. What I want it to do is to bind to the *instance* of
the User object that I have created in the wizard - since the Program
assignments haven't been saved to the database yet, this is the only place to
retrieve this data from.

So my questions are:

1. Is there any way to bind an ObjectDataSource to an instance of a custom
object?

2. Is there a better way of attacking this problem? (I have other ideas,
but they will all required substantially more coding. But perhaps I'm going
about this all wrong...)

Thank you,
Greg
 
B

Brennan Stehling

You can bind your instance to the GridView with this code...

users = GetCollectionOfUsers();
GridView1.DataSource = users;
GridView1.DataBind();

Your user object should expose values through Properties and you can
bind to them by name in columns for the GridView.

Is this not your approach?

Be sure you are not trying to bind a single instance of a user object
to a GridView. A GridView is meant for lists. To bind a single user
instance, consider using a DetailsView or FormView control.

Brennan Stehling
http://brennan.offwhite.net/blog/
 
G

Guest

Excellent!!

I knew you could bind the GridView to a DataTable in the code like that, but
I didn't realize you could bind it directly to a List as well.

And to think I was about to try something a lot more complicated...

Thanks a lot!
Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top