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

Get Editor Text/Value from GridViewComboBoxColumn with DropDownStyle = DropDown

4 Answers 376 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Holger Boskugel
Top achievements
Rank 2
Holger Boskugel asked on 18 Apr 2012, 09:59 AM
Hello,

I'm using a GridViewComboBoxColumn with DropDownStyle = DropDown, but the underlaying RadDropDownListEditor don't assing the typed text from text area into cell value. Only entries from the list are placed in cell value. Datasource behind the combobox is a List<string>. I want do two things:

a) Get the editor working to assing also value not placed in list to cell value
b) Get text from editor text area to be able checking agains data list and adding to list for next use of editor

Regards from Leipzig

Holger Boskugel

4 Answers, 1 is accepted

Sort by
0
Accepted
Nikolay
Telerik team
answered on 18 Apr 2012, 11:21 AM
Hello Holger,

We have an article which exactly covers your scenario. It allows the end-user to type text freely and if the value does not exists in the list of available values, this value is added to the list and committed to the cell:
http://www.telerik.com/help/winforms/gridview-editors-howto-allow-end-users-to-add-items-to-dropdownlisteditor.html 

I hope this helps.

Kind regards,
Nikolay
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
Accepted
Holger Boskugel
Top achievements
Rank 2
answered on 20 Apr 2012, 10:45 AM
Thanks, got me started on right way to my DropDownListAddEditor, see class below

/// <summary>
/// Rad controls drop down editor with InputValueNotFound event
/// </summary>
internal class DropDownListAddEditor :
                   RadDropDownListEditor
{
    protected GridDataCellElement cell;
    protected InputValueNotFoundArgs e;
    /// <summary>
    /// Event handler for missing values in item list of editor
    /// </summary>
    /// <param name="sender">Event source of type DropDownListAddEditor</param>
    /// <param name="e">Event arguments</param>
    public delegate void InputValueNotFoundHandler(object sender,
                                                   InputValueNotFoundArgs e);
    /// <summary>
    /// Event for missing values in item list of editor
    /// </summary>
    public event InputValueNotFoundHandler InputValueNotFound;
    /// <summary>
    /// Constructor
    /// </summary>
    public DropDownListAddEditor() :
               base()
    {
        // Nothing to do
    }
    public override bool EndEdit()
    {
        RadDropDownListEditorElement element = this.EditorElement as RadDropDownListEditorElement;
        string text = element.Text;
        RadListDataItem item = null;
        foreach (RadListDataItem entry in element.Items)
        {
            if (entry.Text == text)
            {
                item = entry;
                break;
            }
        }
        if ((item == null) &&
           (InputValueNotFound != null))
        {
            // Get cell for handling CellEndEdit event
            this.cell = (this.EditorManager as GridViewEditManager).GridViewElement.CurrentCell;
            // Add event handling for setting value to cell
            (this.OwnerElement as GridComboBoxCellElement).GridControl.CellEndEdit += new GridViewCellEventHandler(OnCellEndEdit);
            this.e = new InputValueNotFoundArgs(element);
            this.InputValueNotFound(this,
                                    this.e);
        }
        return base.EndEdit();
    }
    /// <summary>
    /// Puts added value into cell value
    /// </summary>
    /// <param name="sender">Event source of type GridViewEditManager</param>
    /// <param name="e">Event arguments</param>
    /// <remarks>Connected to GridView event CellEndEdit</remarks>
    protected void OnCellEndEdit(object sender,
                                 GridViewCellEventArgs e)
    {
        if (this.e != null)
        {
            // Handle only added value, others by default handling of grid
            if ((this.cell == (sender as GridViewEditManager).GridViewElement.CurrentCell) &&
                this.e.ValueAdded)
            {
                e.Row.Cells[e.ColumnIndex].Value = this.e.Value;
            }
            this.e = null;
        }
    }
    /// <summary>
    /// Event arguments for InputValueNotFound
    /// </summary>
    public class InputValueNotFoundArgs :
                     EventArgs
    {
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="editorElement">Editor assiciated element</param>
        internal protected InputValueNotFoundArgs(RadDropDownListEditorElement editorElement)
        {
            this.EditorElement = editorElement;
            this.Text = editorElement.Text;
        }
        /// <summary>
        /// Editor associated element 
        /// </summary>
        public RadDropDownListEditorElement EditorElement { get; protected set; }
        /// <summary>
        /// Input text with no match in drop down list
        /// </summary>
        public string Text { get; protected set; }
        /// <summary>
        /// Text related missing value
        /// </summary>
        /// <remarks>Has to be set during event processing</remarks>
        /// <seealso cref="ValueAdded"/>
        public object Value { get; set; }
        /// <summary>
        /// Missing value added
        /// </summary>
        /// <remarks>Set also the Value property</remarks>
        public bool ValueAdded { get; set; }
    }
}

And implementation in underlaying Form class is quiet easy:

private void GridView_EditorRequired(object sender, EditorRequiredEventArgs e)
{
    GridViewEditManager manager = sender as GridViewEditManager;
    // Assigning DropDownListAddEditor to the right column
    if (manager.GridViewElement.CurrentColumn.Name == "SPSite")
    {
        DropDownListAddEditor editor = new DropDownListAddEditor();
        editor.InputValueNotFound += new DropDownListAddEditor.InputValueNotFoundHandler(DropDownListAddEditor_InputValueNotFound);
        e.Editor = editor;
    }
}
private void DropDownListAddEditor_InputValueNotFound(object sender, DropDownListAddEditor.InputValueNotFoundArgs e)
{
    if (!string.IsNullOrEmpty(e.Text))
    {
        List<string> values = e.EditorElement.DataSource as List<string>;
        values.Add(e.Text);
        e.Value = e.Text;
        e.ValueAdded = true;
    }
}

Hope it helps others.

Regards

Holger
..
http://vbwebprofi.de
0
Nikolay
Telerik team
answered on 20 Apr 2012, 11:58 AM
Hello Holger,

Thank you for sharing your solution with the community. I believe that it will be helpful to those interested in the topic.

Let us know if you have additional questions or feedback.

All the best,
Nikolay
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
Michael Bakker
Top achievements
Rank 2
answered on 05 Dec 2014, 10:39 AM
Thanks for sharing!
Tags
GridView
Asked by
Holger Boskugel
Top achievements
Rank 2
Answers by
Nikolay
Telerik team
Holger Boskugel
Top achievements
Rank 2
Michael Bakker
Top achievements
Rank 2
Share this question
or