Editable Combobox

10 posts, 0 answers
  1. Silvina P.
    Silvina P. avatar
    3 posts
    Member since:
    Jan 2010

    Posted 22 Jan 2010 Link to this post

    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();

     

  2. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 27 Jan 2010 Link to this post

    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.
  3. Silvina P.
    Silvina P. avatar
    3 posts
    Member since:
    Jan 2010

    Posted 08 Feb 2010 Link to this post

    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.

     

  4. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 11 Feb 2010 Link to this post

    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.
  5. Ramin
    Ramin avatar
    30 posts
    Member since:
    Sep 2010

    Posted 30 Mar 2012 Link to this post

    Hi Pavel,
    I have exactly the same issue. What was the solution?

    Thanks
    Ramin
  6. Albert
    Albert avatar
    1 posts
    Member since:
    Dec 2011

    Posted 22 Oct 2012 Link to this post

    Hello,

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

    Kind Regards,
    Albert
  7. Markus Hopfenspirger
    Markus Hopfenspirger avatar
    27 posts
    Member since:
    Oct 2009

    Posted 16 May 2014 Link to this post

    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
  8. Yoan
    Admin
    Yoan avatar
    1213 posts

    Posted 20 May 2014 Link to this post

    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.
     
  9. Markus Hopfenspirger
    Markus Hopfenspirger avatar
    27 posts
    Member since:
    Oct 2009

    Posted 30 Sep 2014 in reply to Yoan Link to this post

    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);
    }
    }
    }
  10. Yoan
    Admin
    Yoan avatar
    1213 posts

    Posted 03 Oct 2014 Link to this post

    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.
     
Back to Top