This is a migrated thread and some comments may be shown as answers.

Bind ComboBox in code-behind

2 Answers 144 Views
Grid
This is a migrated thread and some comments may be shown as answers.
TonyG
Top achievements
Rank 1
TonyG asked on 30 Aug 2010, 10:06 AM
I hope I'm not too vague here and I'll be happy to post a project or just explain a bit better if required.

I have a simple grid with two columns. I am databinding the grid with NeedsDataSource, dynamically creating a DataTable in code-behind from a business object. I am trying to completely avoid using ObjectDataSources.  In short:

Column1 = Entity.EmailAddress (example: abc@def.com)
Column2 = Entity.EmailAddressType.Description

EmailAddressType is an object of EmailAddressType with properties ID and Description. (example: hm=Home)

The grid simply allows someone to enter a number of email addresses, and what they're for: Home, Work, School, etc., with the type limited to a provided selection.

The ItemTemplate for column2 renders "Home". The EditItemTemplate presents a RadComboBox, also generated at runtime, with all EmailAddressTypes loaded with Value=type.ID and Text=type.Description.

At runtime, I can elect to Insert or Update, and the ComboBox renders the list. Everything looks good until changes are submitted.

The problem is that when I change the data and click Update, the RadGrid ItemUpdated event is fired, but only the first column with the address is available in the event e.Item.  I thought the selected value from the second column EditItemTemplate would be available there too.

I don't think I'm properly defining the RadComboBox within the EditItemTemplate.  Do I need to check the combobox itself to get the value?  (Not sure it's available when ItemUpdated fires)  How do I ensure that the value of the combobox is associated with the cells in the row being changed so that when postback events are fired the selected value comes back with all other data?  (I haven't tried but I suspect I'm going to have the same problem with other controls, so what I learn here will be very helpful for subsequent work.

As to why I want to avoid ObjectDataSource: It's just too flaky, not powerful enough, always leaving something to be desired somewhere between markup and codebehind. Read notes by Nikhil Khotari, Manuel Abadia, and many others. So I'm trying to get some practice doing everything programmatically, and unfortunately sacrificing some of the designer benefits of binding to a datasource at design time.

Thanks!

2 Answers, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 02 Sep 2010, 09:56 AM
Hello TonyG,

The ItemUpdated event of the grid is fired when automatic CRUD operations are allowed and the item update has finished. However automatic operations are posible only in case you bound the grid to a DataSource control. When the grid is bound through the NeedDataSource event, you need to perform the update manually. For that purpose turn the AllowAutomaticUpdates property of the grid back to false and handle the grid UpdateCommand/ItemCommand. You can check out this article for a reference.

Sincerely yours,
Iana
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
TonyG
Top achievements
Rank 1
answered on 02 Sep 2010, 03:45 PM
Iana, thanks for the response. Based on other forum postings here I actually did exactly as you suggest here. I've created a nice stand-alone project that populates a grid and embedded combo in code-behind, and processes all CRUD with in-line editing.  I'll submit it for review soon.  Perhaps our friends here can use it as a learning tool, and others can comment to help us all improve our understanding and coding style.
Tags
Grid
Asked by
TonyG
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
TonyG
Top achievements
Rank 1
Share this question
or