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

How to make GridViewComboboxColumn editable?

1 Answer 121 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Yipeng
Top achievements
Rank 1
Yipeng asked on 11 Oct 2018, 03:55 AM

Hi,

I have a GridViewComboboxColumn, I want to make it can select the value from the drop down list and also can be editted mannually. Following is the code:

<telerik:GridViewComboBoxColumn Header="Tool Name" DataMemberBinding="{Binding ToolName}" 
                                                    UniqueName="ToolName"
                                                    FilterMemberPath="DisplayName"
                                                    SelectedValueMemberPath="Name"
                                                    DisplayMemberPath="DisplayName"
                                                    ItemsSource="{Binding ToolNames}"

                                                    IsComboBoxEditable="True">

 

But after I manually edited the combobox, and click other cell, the combobox becomes empty. Can anyone help? Thanks!

 

1 Answer, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 12 Oct 2018, 12:00 PM
Hello Yipeng,

The reason no value is displayed in the combo box once you input a new value is that this value is not actually added to its ItemsSource and thus null is set as the value of your underlying property. You need to manually insert this value in the collection.

What I can suggest is to handle the CellEditEnded event in a similar fashion:

private void Grid_CellEditEnded(object sender, GridViewCellEditEndedEventArgs e)
{
    var column = e.Cell.Column as GridViewComboBoxColumn;
    if (column != null)
    {
        if (e.NewData == null && e.EditAction == GridViewEditAction.Commit)
        {
            var combo = e.EditingElement as RadComboBox;
            var text = combo.Text;
            var source = combo.ItemsSource as IList<Tool>;
            var last = source.Last();
            source.Add(new Country() { Name = text, DisplayName = text });
            var item = e.Cell.DataContext;
            item.GetType().GetProperty(column.DataMemberBinding.Path.Path).SetValue(item, text);
        }
    }
}

As I'm unaware of your exact setup, here I assume that your combobox column is bound to a property of type Tool which has Name and DisplayName properties of type string. In addition, your ItemsSource should implement the IList<T> interface.

I've also attached a small sample project with a similar approach in action.

Please have a look and let me know if you're able to use such an approach at your end. I look forward to your reply.

Regards,
Dilyan Traykov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
GridView
Asked by
Yipeng
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
Share this question
or