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

Editable Combobox

9 Answers 424 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Silvina P.
Top achievements
Rank 1
Silvina P. asked on 22 Jan 2010, 03:30 PM
Hello,
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

Sort by
0
Pavel Pavlov
Telerik team
answered on 27 Jan 2010, 10:00 AM
Hello Silvina P.,

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.
0
Silvina P.
Top achievements
Rank 1
answered on 08 Feb 2010, 10:18 PM
Thank you so much for your response! I added the following code to the CellEditEnded event

 

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.

 

0
Pavel Pavlov
Telerik team
answered on 11 Feb 2010, 03:19 PM
Hello Silvina P.,

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.
0
Ramin
Top achievements
Rank 1
answered on 30 Mar 2012, 06:01 PM
Hi Pavel,
I have exactly the same issue. What was the solution?

Thanks
Ramin
0
Albert
Top achievements
Rank 1
answered on 22 Oct 2012, 08:27 PM
Hello,

I am also having the same problem. What was the solution?

Kind Regards,
Albert
0
Markus Hopfenspirger
Top achievements
Rank 2
answered on 16 May 2014, 08:26 AM
What was the solution? I have the same issue?

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
0
Yoan
Telerik team
answered on 20 May 2014, 03:15 PM
Hi 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
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Markus Hopfenspirger
Top achievements
Rank 2
answered on 30 Sep 2014, 12:06 PM
Hello Yoan,

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);
}
}
}
0
Yoan
Telerik team
answered on 03 Oct 2014, 09:08 AM
Hi Markus,

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
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
GridView
Asked by
Silvina P.
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
Silvina P.
Top achievements
Rank 1
Ramin
Top achievements
Rank 1
Albert
Top achievements
Rank 1
Markus Hopfenspirger
Top achievements
Rank 2
Yoan
Telerik team
Share this question
or