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

Editable GridViewComboBoxColumn

2 Answers 355 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Troy
Top achievements
Rank 1
Troy asked on 07 Jan 2012, 01:15 AM
I need to have a column in the grid where a user can either select a value from a list or type in a value. For example, if the user is enter a face amount for a life insurance policy the user can either select one of these three options: Minimum, Maximum, Target or enter a number from 100,000 to 10,000,000.

I tried using the GridViewComboBoxColumn and setting the DropDownStyle to DropDown. This allowed me to edit the value in the cell, but when I leave the cell, the cell's display value flips back to what it was previously.

In the example attached, click on the first row where it says 'Minimum' and type in 100000, then hit the tab key to move to a new cell. The cell flips from 100000 back to 'Minimum'.

Is there a way to use the GridViewComboBoxColumn to allow the user to either select a value from the drop down list or type in a value?

Here is the Form1.Designer.cs file:

namespace GridComboBoxColumn
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        #region Windows Form Designer generated code
 
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this._grid = new Telerik.WinControls.UI.RadGridView();
            ((System.ComponentModel.ISupportInitialize)(this._grid)).BeginInit();
            this.SuspendLayout();
            //
            // _grid
            //
            this._grid.Location = new System.Drawing.Point(13, 13);
            this._grid.Name = "_grid";
            this._grid.Size = new System.Drawing.Size(259, 237);
            this._grid.TabIndex = 0;
            this._grid.Text = "radGridView1";
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this._grid);
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this._grid)).EndInit();
            this.ResumeLayout(false);
 
        }
 
        #endregion
 
        private Telerik.WinControls.UI.RadGridView _grid;
 
    }
}

Here is the sample Form1.cs file:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
using Telerik.WinControls.Primitives;
using Telerik.WinControls.UI;
 
namespace GridComboBoxColumn
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
 
            SetupGrid();
        }
 
        private void SetupGrid()
        {
            // Add Column
            {
                GridViewComboBoxColumn column = new GridViewComboBoxColumn();
                column.Name = "Policy.FaceAmount";
                column.HeaderText = "Total Face Amount";
                column.ValueMember = "Value";
                column.DisplayMember = "Name";
                column.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDown;
                column.ReadOnly = false;
 
                BindingList<QuestionSelectionItem> list = new BindingList<QuestionSelectionItem>();
 
                list.Add(new QuestionSelectionItem("Minimum", "MIN"));
                list.Add(new QuestionSelectionItem("Maxmimum", "MAX"));
                list.Add(new QuestionSelectionItem("Target", "TGT"));
 
                column.DataSource = list;
 
                _grid.Columns.Add(column);
            }
 
            // Add Row
            {
                // First Row
                {
                    GridViewRowInfo row = _grid.Rows.AddNew();
 
                    row.Cells["Policy.FaceAmount"].Value = "MIN";
                }
 
                // Second Row
                {
                    GridViewRowInfo row = _grid.Rows.AddNew();
 
                    row.Cells["Policy.FaceAmount"].Value = "100000";
                }
 
                // Third Row
                {
                    GridViewRowInfo row = _grid.Rows.AddNew();
 
                    row.Cells["Policy.FaceAmount"].Value = "MAX";
                }
            }
        }
 
        protected class QuestionSelectionItem
        {
            private string _name = null;
            private string _value = null;
 
            public QuestionSelectionItem(string aName, string aValue)
            {
                _name = aName;
                _value = aValue;
            }
 
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
 
            public string Value
            {
                get { return _value; }
                set { _value = value; }
            }
        }
    }
}

2 Answers, 1 is accepted

Sort by
0
Accepted
Ivan Petrov
Telerik team
answered on 11 Jan 2012, 05:37 PM
Hi Troy,

Thank you for writing.

You can have a look at the following help article - Allow end-users to add items to DropDownListEditor. It explains how to achieve your scenario.

I hope this will be useful for you. Should you have further questions, I would be glad to assist.

Regards,
Ivan Petrov
the Telerik team

SP1
of Q3’11 of RadControls for WinForms is available for download (see what's new).
0
Troy
Top achievements
Rank 1
answered on 20 Jan 2012, 11:10 PM
This worked for me. The only issue was that I didn't want the user entered values to appear in the drop down list. To workaround this, I used the filter method to filter out the items that I didn't want to appear in the drop down list.
Tags
GridView
Asked by
Troy
Top achievements
Rank 1
Answers by
Ivan Petrov
Telerik team
Troy
Top achievements
Rank 1
Share this question
or