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

ComboBox selecteditem dataitem null

3 Answers 708 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Jeremy
Top achievements
Rank 1
Jeremy asked on 06 Sep 2017, 10:41 PM

I'm using a RadComboBox and would like to get the selected item that was bound to the combobox instead of the selected value, index or text.  I can do that with RadGrid but when I try to do it with a ComboBox the selected item's dataitem is always null.

RadGrid:

Event thisEvent = new Event();                  
thisEvent = (Event)e.Item.DataItem;

 

thisEvent is now the bound record of type Event.

 

RadComboBox:

List<BWGroup> individuals = new List<BWGroup>();
individuals = _bwContext.BWGroups.Where(x => x.IsActive == true).OrderBy(x => x.BWGroupName).ToList();
 IndividualsRadComboBox.DataTextField = "BWGroupName";
IndividualsRadComboBox.DataValueField = "BWGroupID";
IndividualsRadComboBox.DataSource = individuals;
 IndividualsRadComboBox.DataBind();
 
//After event is fired
 BWGroup group = new BWGroup();
 group = IndividualsRadComboBox.SelectedItem.DataItem as BWGroup;

 

SelectedValue and SelectedIndex have the proper values but DataItem is always null so group is always null.

3 Answers, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 12 Sep 2017, 07:01 AM
Hi Jeremy,

The DataItem instance of the item of any control can be accessed only during binding action, namely in the corresponding ItemDataBound event handler of the given control.

Regards,
Eyup
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Jeremy
Top achievements
Rank 1
answered on 12 Sep 2017, 01:57 PM
So you're saying you have access to the data item when you're binding it and only when you're binding it.  After the control is bound and rendered you can't actually get the selected data item out of the control and have to rely on a string like value or text to then go back and find your original data item instead of just making it persist? So once I actually want to get the data item selected from a RadComboBox, I still have to hit the database again even though the control once stored that data item because now apparently it doesn't fee like storing it anymore.  I will do my best to be polite here and just say "that's silly" instead of what I want to say.
0
Eyup
Telerik team
answered on 15 Sep 2017, 02:12 PM
Hi Jeremy,

Yes, this is correct. And intentionally done for optimization. Otherwise, image all the data in all of the combo items going back and forth the ViewState. This will cause a huge load unnecessarily.

What you can do is to get the raw data during ItemDataBound and then add it as some custom Attribute of the item, which you can later access and extract:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/rows/accessing-cells-and-rows#accessing-raw-field-data-and-key-values

Regards,
Eyup
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
ComboBox
Asked by
Jeremy
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Jeremy
Top achievements
Rank 1
Share this question
or