ComboBox with 'entities'

4 posts, 2 answers
  1. Brian
    Brian avatar
    14 posts
    Member since:
    Oct 2010

    Posted 28 Sep 2011 Link to this post

    Just getting started with Telerik controls...

    I need to get the behavior of the RadDropDownList for a column in a GridView.

    For the RadDropDownList, I am using databinding to link the SelectedValue to the property of a Business Object or Entity.

    I'm trying the same approach in the GridView but no luck.

    I have added a GridViewComboBox column and set its DataSource to the list of entities I want in the drop down list.  That works fine, and when displaying data the correct item is highlighted.  However, trying to pick a new item from the list gives this error: "Object of type 'System.String' cannot be converted to 'mynamespace.Product'.

    I can see in the docs it wants me to set the ValueMember to the property on the data source that I want to use, but I don't want to use a property, I want the whole object.  How can I do this??

    I found this other post and the poster was in the same situation as I am:
    http://www.telerik.com/community/forums/winforms/gridview/csla-bindinglist-with-default-objects.aspx

    It is from 2010 - so has anything changed or is it still not supported?  Do I still have to make up some kind of workaround?
  2. Brian
    Brian avatar
    14 posts
    Member since:
    Oct 2010

    Posted 29 Sep 2011 Link to this post

    I'm trying to see if the workaround will work for me. I had to update it a little since it appears that the GridView doesn't use a combo box as an editor anymore.  Just updating to the RadDropDownListEditor didn't work though, the SelectedItem was still the old value.  So I found that in the EventArgs, I was being given the OldValue and NewValue.  So I changed the code to simply see which column was being changed, and then to put the NewValue into the object.

    It does change the value on the object, but the displayed text on the combo box is the old value.  It doesn't change until I click on another cell, then it updates to the correct text.  To work around that, I added code to automatically move to the next column after updating the value on the entity.  Is there a better way?

    private void grdProducts_ValueChanging(object sender, ValueChangingEventArgs e)
    {
        if (grdProducts.CurrentColumn.FieldName == "Product")
        {
            var chemicalRecProduct = grdProducts.CurrentRow.DataBoundItem as ChemicalRecProduct;
            chemicalRecProduct.Product = e.NewValue as Product;
     
            e.Cancel = true;
            grdProducts.CurrentRow.InvalidateRow();  //This doesn't appear to refresh the drop down

               // move focus to next column, to force the drop down to update its display
               grdProducts.CurrentColumn = grdProducts.Columns[grdProducts.CurrentColumn.Index + 1];
        }
    }

  3. UI for WinForms is Visual Studio 2017 Ready
  4. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 30 Sep 2011 Link to this post

    Hello Brian,

    To force the grid to apply changes you should call radGridView.EndEdit();

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga

    Telerik WinForms MVP
  5. Answer
    Peter
    Admin
    Peter avatar
    1148 posts

    Posted 30 Sep 2011 Link to this post

    Hello Brian,

    Thank you for writing.

    On ValueChanging event the SelectedItem has still the old value, because the Value is still not changed. The Value is changed on the ValueChanged event. So, you can consider using the ValueChanged event instead of the ValueChanging event or continue using the ValueChanging's argument NewValue.

    You can use the Emanuel's solution and call EndEdit() to force the grid to apply your change.

    I hope this helps.

    Greetings,

    Peter
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top