Get Editor Text/Value from GridViewComboBoxColumn with DropDownStyle = DropDown

5 posts, 2 answers
  1. Holger Boskugel
    Holger Boskugel avatar
    24 posts
    Member since:
    Apr 2012

    Posted 18 Apr 2012 Link to this post

    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
  2. Answer
    Nikolay
    Admin
    Nikolay avatar
    1804 posts

    Posted 18 Apr 2012 Link to this post

    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 >>
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Answer
    Holger Boskugel
    Holger Boskugel avatar
    24 posts
    Member since:
    Apr 2012

    Posted 20 Apr 2012 Link to this post

    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
  5. Nikolay
    Admin
    Nikolay avatar
    1804 posts

    Posted 20 Apr 2012 Link to this post

    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 >>
  6. Michael Bakker
    Michael Bakker avatar
    8 posts
    Member since:
    Jun 2012

    Posted 05 Dec 2014 in reply to Holger Boskugel Link to this post

    Thanks for sharing!
Back to Top
UI for WinForms is Visual Studio 2017 Ready