473,738 Members | 8,848 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 ObjectDataSourc e.
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 ObjectDataSourc e. Methods to
insert, update, and delete are not too complicated either.

The problem is that it appears that the ObjectDataSourc e 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 ObjectDataSourc e 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 8990
You can bind your instance to the GridView with this code...

users = GetCollectionOf Users();
GridView1.DataS ource = users;
GridView1.DataB ind();

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 ObjectDataSourc e.
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 ObjectDataSourc e. Methods to
insert, update, and delete are not too complicated either.

The problem is that it appears that the ObjectDataSourc e 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 ObjectDataSourc e 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 = GetCollectionOf Users();
GridView1.DataS ource = users;
GridView1.DataB ind();

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 ObjectDataSourc e.
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 ObjectDataSourc e. Methods to
insert, update, and delete are not too complicated either.

The problem is that it appears that the ObjectDataSourc e 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 ObjectDataSourc e 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
18540
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" ConnectionString="<%$ ConnectionStrings:cnnSQL_Connect %>" Runat="server"> <SelectParameters> <asp:Parameter Type="String" DefaultValue="FindWebDocAll"
3
3483
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 I can do with databinding. thanks Hans Merkl
8
5039
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 select, remember it, and then use it in the update. It works just fine when I have full control of the whole process. I want to do the same for my GridView/SqlDataSource combinations. I typically select from a view and update the corresponding...
19
2343
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 object to databind to text boxes etc? I can't use a dataset as the object has loads of derived logic, for example updating one property may actually update several database fields for example.
0
3036
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 postback set to true as I need to refresh the 2nd and 3rd list. Then clicks on Submit button which fires a custom event "FilterSubmitted". Yes this a custom control, as I will be using it in many places. I have a GridView control on the page that...
5
3005
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 list that corresponds with the number of pages, but I am unable to wire up the Selected Index Changed event. Auto PostBack is set to true and the page is posting back when the DDL is selected / chaged, but the event is never being called. Any...
8
11970
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 which is not a string, for example a custom type Address which contains properties such as StreetAddress, City, County, Country etc how do i bind to those properties on the same form view? I have tried doing Bind("Address.StreetAddress") but...
0
1049
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 to populate the datatable. When I bind a custom business object to the gridview control, the column order is not the same as the order in which the business object
0
1210
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 client. One of the client program (ASP.NET) is trying to bind this object to a gridview. In gridview, this list is turned into MyObject. My question is (a) why is this transformation happening. (b) Is it something to do with the gridview design (c)...
0
8788
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9476
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9263
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8210
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6751
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.