473,399 Members | 3,038 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 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 8967
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.