How is the IsComboBoxEditable property works? I need to present a grid where one column is a combobox. The user can select one value from the list or enter a new value so I set the IsComboBoxEditable to true but the problem is that if the user types a value that is not in the ItemsSource the Text is blank when the combobox looses the focus. Can you please tell me what I'm doing wrong?
<Telerik:GridViewComboBoxColumn Header="Location" Width="110" IsComboBoxEditable="True"
SelectedValueMemberPath="Name"
DisplayMemberPath="Name"
DataMemberBinding="{Binding [Location]}"/>
In code I bind the Itemsource to the available list.
((
GridViewComboBoxColumn)tblTrain.Columns[2]).ItemsSource = GetTrainToFacilities();
9 Answers, 1 is accepted
By default the ComboBox column will work only with values present in the ItemsSource.
I guess for your scenario, you need when a new value is typed , this value to be added to the itemssource.
This is not an automatic featureof the combobox column . You can implement it in a few lines of code the following way :
1. Subscribe to the CellEditEnded event of RadGridView
2. Within the event handler write your own logic to add thew newly typed item to the collection.
Spomething like :
this
.RadGridView1.CellEditEnded +=
new
EventHandler<GridViewCellEditEndedEventArgs>(RadGridView1_CellEditEnded);
}
void
RadGridView1_CellEditEnded(
object
sender, GridViewCellEditEndedEventArgs e)
{
//here you may need to perform a check which column is being edited to be sure it is the one with the combobox
// this may be done by checking the e.Cell.Column
string
newlyTypedText = ((RadComboBox)e.EditingElement).Text;
//here add the newlyTypedText item to the ComboBoxColumn.ItemsSource collection
}
Let me know if you find troubles implementig this approach into your project.
Sincerely yours,
Pavel Pavlov
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
if (e.Cell.Column.UniqueName == "Location")
{
List<Facility> Facilities = new List<Facility>();
foreach (Facility fac in ((GridViewComboBoxColumn)tblTrain.Columns[3]).ItemsSource)
{
Facilities.Add(fac);
}
Facility newLoc = new Facility();
newLoc.Name = (string)((RadComboBox)e.EditingElement).Text;
Facilities.Add(newLoc);
}
The only issue that I faced is that the Text value for the cell is empty after I loose focus first time. It works when I select the recently added item from the list.
What else do I need to do so the cell value is not null after I add the Itemsource to the list?
Thanks!
Silvina.
Without having your code I can only guess what causes the problem. However - it my be helpful if you implement the INotifyPropertyChanged interface on the Facility object. This way RadGridView will automatically update the cell at the moment the value is changed and this might fix the problem.
If it does not , please open a support ticket and send me the project. i will gladly fix it for you .
Best wishes,
Pavel Pavlov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
I have exactly the same issue. What was the solution?
Thanks
Ramin
I am also having the same problem. What was the solution?
Kind Regards,
Albert
Also the NewValue of the EventArg ist empty...??? (The Old Value is filled)
(And I'm using MVVM with INotifyPropertyChanged and everything...)
Is there no better solution for such a simple Scenario?
I mean, binding a Combobox to a List<string> and allow the user to select one of the values or type in anything is a very Basic usage of a Combobox. Is'n it? And also the Column does not Display the old values if they are not in the list...???
?????????????????
Kind regards,
Markus
I would suggest you to check the Combobox Column Editable github example. It demonstrates how to use GridViewComboBoxColumn and allow the user to edit it and add new items for its ItemsSource.
As a side note - Although GitHub is a very well-known platform we saw a better and easier approach for reviewing our examples developing our SDK Samples Browser. You can also use it to review the examples.
Regards,
Yoan
Telerik
thanks for your post and sorry for the late reply, but somehow i missed the notification...
Sorry, but putting code in den code behind file, wherever I need this behavior is no solution for me. A workaround for me is to define a custom ColumnType which displays a normal RadComboBox as an editor...
(I'm still wondering why the default editor of the GridViewComboBoxColumn can not handle this simple Scenario...???)
kind regards,
Markus
Here is my code
public class RadGridViewSimpleComboBoxColumn : GridViewComboBoxColumn {
private object _dataItem;
private RadComboBox _combobox;
public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem) {
var box = cell.Content as TextBlock;
if (box == null) {
box = new TextBlock {TextAlignment = TextAlignment};
box.SetBinding(TextBlock.TextProperty, DataMemberBinding);
}
return box;
}
public override FrameworkElement CreateCellEditElement(GridViewCell cell, object dataItem) {
_dataItem = dataItem;
_combobox = new RadComboBox { ItemsSource = ItemsSource, IsEditable = IsComboBoxEditable };
_combobox.LostFocus += ComboBoxLostFocus;
var prop = _dataItem.GetType().GetProperty(DataMemberBinding.Path.Path);
if (prop != null) {
if (_combobox.IsEditable) {
_combobox.Text = prop.GetValue(_dataItem) as string;
} else {
_combobox.SelectedItem = prop.GetValue(_dataItem);
}
}
return _combobox;
}
private void ComboBoxLostFocus(object sender, RoutedEventArgs e) {
var prop = _dataItem.GetType().GetProperty(DataMemberBinding.Path.Path);
if (prop == null) return;
if (_combobox.IsEditable) {
prop.SetValue(_dataItem, _combobox.Text);
} else {
prop.SetValue(_dataItem, _combobox.SelectedItem);
}
}
}
I am glad to hear that the problem is resolved.
As for your question: The thing is that GridViewComboBoxColumn was built to serve to very specific scenario. For all other custom requirements we had provided a way to specify a CellTemplate/CellEditTemplate for a column.
Regards,
Yoan
Telerik