ComboBox selecteditem dataitem null

4 posts, 0 answers
  1. Jeremy
    Jeremy avatar
    36 posts
    Member since:
    Apr 2014

    Posted 06 Sep 2017 Link to this post

    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.

  2. Eyup
    Admin
    Eyup avatar
    3962 posts

    Posted 12 Sep 2017 Link to this post

    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.
  3. Jeremy
    Jeremy avatar
    36 posts
    Member since:
    Apr 2014

    Posted 12 Sep 2017 in reply to Eyup Link to this post

    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.
  4. Eyup
    Admin
    Eyup avatar
    3962 posts

    Posted 15 Sep 2017 Link to this post

    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.
Back to Top