472,989 Members | 3,007 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,989 software developers and data experts.

Binding a GridView to a custom object

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
Oct 26 '06 #1
2 8940
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/

Greg wrote:
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
Oct 26 '06 #2
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
"Brennan Stehling" wrote:
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/

Greg wrote:
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

Oct 26 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: sck10 | last post by:
Hello, I have a GridView that is using the following to connect to a SQL Server 2000 stored procedure: <asp:SqlDataSource ID="dsWebDocList" SelectCommand="sp_web_WebDocument"...
3
by: Hans Merkl | last post by:
Hi, I was wondering if it's possible to bind the header text of a GridView column to a method of an object I have. At the moment I am setting the header texts in Page_Load but I was wondering if...
8
by: Mike Kelly | last post by:
I've chosen to implement the "optimistic concurrency" model in my application. To assist in that, I've added a ROWVERSION (TIMESTAMP) column to my main tables. I read the value of the column in my...
19
by: Simon Verona | last post by:
I'm not sure if I'm going down the correct route... I have a class which exposes a number of properties of an object (in this case the object represents a customer). Can I then use this...
0
by: bsaran | last post by:
Hi, I have a code that sort of works. The event flow is as follows: In a user control of type "FilterControl" user selects certain values from 3 related dropdown lists. (two of them have auto...
5
by: Andrew Robinson | last post by:
I am attempting to better automate a Pager Template within a GridView. I am succesfully skinning a Drop Down List withing my control (the DDL is added to my control). I correctly populate the item...
8
by: =?Utf-8?B?QXNo?= | last post by:
Hi, I have an object, for example User. User contains various properties which i have been able to bind to successfully using wizards and the form view. However if the class User has a property...
0
by: hharry | last post by:
Hello All, I am binding a custom business object the gridview control. When I bind a datatable to the gridview control, the columns are displayed in the same order as the select statement used...
0
by: manig16 | last post by:
Hello, I have a ASP.NET webservice, that returns a custom object (List<MyObject>). The webservice inturn invokes an assembly which returns List<MyObject>. This object is returned back to the...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.