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

GridViewComboBoxColumn: let user add values that are not in datasource

3 Answers 359 Views
GridView
This is a migrated thread and some comments may be shown as answers.
strombringer
Top achievements
Rank 2
strombringer asked on 16 Sep 2009, 10:38 AM
Hi,

I have a GridView with a GridViewComboBoxColumn that has a list of strings as the datasource and is bound to a string property in my ViewModel. Is it possible to let the user add new values to the string-list by typing in the GridViewComboBoxColumn editor and not reject those values when they are not already in the list?

At the moment, when I type a string that is not already in the bound list, the string is saved in the editor (when I enter the cell again after leaving it, the value is still there) but not propagated through the ValueChanging event and also not written to the underlying ViewModel.

I'm looking for a way to either
  1. Add the new value directly to the string-list and to the ViewModel's property
  2. Get notified about the change with the new, not-in-list value and be able to put it in the string-list manually

Is there a way to do this?

Thanks in advance.

3 Answers, 1 is accepted

Sort by
0
Martin Vasilev
Telerik team
answered on 18 Sep 2009, 03:37 PM
Hi strombringer,

Thank you for writing.

RadGridView does not support the described feature out of the box. However, you can make your custom implementation of it. First, you have to change the standard combo box editor with a custom one which accepts values out of its data source. In addition, you have to manually set its value to the inputted text. Here is an example code for a custom combo editor:
class CustomComboBoxEditor : RadComboBoxEditor 
    public override void Initialize(object owner, object value) 
    { 
        base.Initialize(owner, value); 
        if (value == null
        { 
            ((RadComboBoxEditorElement)this.EditorElement).Text = String.Empty; 
        } 
    } 
 
    public override void BeginEdit() 
    { 
        base.BeginEdit(); 
        this.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDown; 
    }         
 
    public override object Value 
    { 
        get 
        { 
            if (base.Value == null && ((RadComboBoxEditorElement)this.EditorElement).Text != String.Empty) 
                return ((RadComboBoxEditorElement)this.EditorElement).Text; 
            else 
                return base.Value; 
        } 
        set 
        { 
            base.Value = value; 
        } 
    } 

After that, you have to change editor in EditorRequiered event and also in CellEndEdit event add the new value to the string list:
RadGridView myGrid; 
List<String> myStrings; 
 
void myGrid_EditorRequired(object sender, EditorRequiredEventArgs e) 
    if (e.EditorType == typeof(RadComboBoxEditor)) 
    { 
        e.EditorType = typeof(CustomComboBoxEditor); 
    } 
 
void myGrid_CellEndEdit(object sender, GridViewCellEventArgs e) 
    if (e.ColumnIndex == 10 && e.Value.ToString() != String.Empty) 
    { 
        bool stringExists = myStrings.Exists(delegate(String str) { return str == e.Value.ToString(); }); 
        if (!stringExists) 
        { 
            myStrings.Add(e.Value.ToString()); 
        } 
    } 

Feel free to modify this example code according to your needs. Do not hesitate to contact me again if you have other questions.

Best wishes,
Martin Vasilev
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
Jon
Top achievements
Rank 1
answered on 13 Nov 2009, 07:52 PM
I've got this working for edits, but on add new row as the user tabs from the previous column into the combobox column the cursor moves on to the next row. (the last 2 columns are both command buttons).

I added an override on EndEdit()

        public override bool EndEdit() 
        { 
            return base.EndEdit(); 
        } 

and then put a suspend on the line and the EndEdit() is getting called right after the BeginEdit().  If I put a suspend on the base.BeginEdit() and hit f5 (continue) the behavior changes and the cursor stays inside the combobox leading me to believe that the tab key is still in the buffer from leaving the previous column and is being consumed by the combobox as well.  Any way to make this stop?

BTW to make it work I had to change the CustomComboBoxEditor a bit

        public override object Value 
        { 
            get 
            { 
                return ((RadComboBoxEditorElement)EditorElement).Text; 
            } 
            set 
            { 
                base.Value = value
            } 
        } 


0
Jack
Telerik team
answered on 18 Nov 2009, 08:15 AM
Hello Jon Masters,

We will address that issue in our upcoming service pack. You can find how to work around the issue in your support ticket (ID 258766). Should you have any other questions, do not hesitate to ask.

All the best,
Jack
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.
Tags
GridView
Asked by
strombringer
Top achievements
Rank 2
Answers by
Martin Vasilev
Telerik team
Jon
Top achievements
Rank 1
Jack
Telerik team
Share this question
or