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

ComboBox with 'entities'

3 Answers 119 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Brian
Top achievements
Rank 1
Brian asked on 29 Sep 2011, 12:09 AM
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?

3 Answers, 1 is accepted

Sort by
0
Brian
Top achievements
Rank 1
answered on 29 Sep 2011, 04:51 PM
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];
    }
}

0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 30 Sep 2011, 05:38 AM
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
0
Accepted
Peter
Telerik team
answered on 30 Sep 2011, 04:40 PM
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 >>

Tags
GridView
Asked by
Brian
Top achievements
Rank 1
Answers by
Brian
Top achievements
Rank 1
Emanuel Varga
Top achievements
Rank 1
Peter
Telerik team
Share this question
or