Gridview ComboBox Column

2 posts, 0 answers
  1. Edson
    Edson avatar
    1 posts
    Member since:
    Apr 2015

    Posted 29 Oct 2015 Link to this post

    Hi,

    I have a gridview with one combobox column that its populated depending of the value entered in column 1. The combobox column is populated as expected, but, if I save the values and open the record and click inside the combobox column the selected value in that row its not the value that comes from the db, it always select the first value of the combobox. And I need to get the saved selected value when I enter into edit mode.

     

    My code

     

    private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
            {
                RadDropDownListEditor listEditor = this.radGridView1.ActiveEditor as RadDropDownListEditor;

                if (listEditor == null)
                {
                    return;
                }

                RadDropDownListEditorElement editorElement = listEditor.EditorElement as RadDropDownListEditorElement;
                editorElement.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                editorElement.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;


                string item = radGridView1.CurrentRow.Cells[0].Value == null ? "" : radGridView1.CurrentRow.Cells[0].Value.ToString();

                var result = SqlClass.GetUOMByItem(item);
                editorElement.DataSource = null;
                editorElement.Items.Clear();
                editorElement.DataSource = result;
                editorElement.DisplayMember = "UOFM";
                editorElement.ValueMember = "UOFM";


                if (editorElement.Items.Count > 0)
                {
                    editorElement.SelectedValue = null;
                    editorElement.SelectedValue = this.radGridView1.CurrentCell.Value;

                }

            }

     

    thanks for your help!!

  2. Hristo Merdjanov
    Admin
    Hristo Merdjanov avatar
    710 posts

    Posted 02 Nov 2015 Link to this post

    Hello Edson,

    Thank you for writing.

    Since you are rebinding the column every time you open the editor, it is expected that the selection in the popup will be lost. Your solution with saving the selected value and setting it after rebinding the grid is valid. I extended the code snippet you sent me to create an example: 
    public partial class Form1 : Form
    {
        private BindingSource bs;
     
        public Form1()
        {
            InitializeComponent();
     
            this.radGridView1.DataSource = this.GetData();
     
            GridViewComboBoxColumn combolCol = new GridViewComboBoxColumn();
            combolCol.Name = "Phone";
            combolCol.Width = 200;
            this.radGridView1.Columns.Add(combolCol);
     
            this.radGridView1.CellEditorInitialized += radGridView1_CellEditorInitialized;
        }
     
        private void radGridView1_CellEditorInitialized(object sender, GridViewCellEventArgs e)
        {
            RadDropDownListEditor listEditor = this.radGridView1.ActiveEditor as RadDropDownListEditor;
            if (listEditor == null)
            {
                return;
            }
     
            RadDropDownListEditorElement editorElement = listEditor.EditorElement as RadDropDownListEditorElement;
            editorElement.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            editorElement.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDownList;
     
            bool isEven = (int)this.radGridView1.CurrentRow.Cells[1].Value % 2 == 0;
            IList<string> res = this.GetComboData(isEven);
            editorElement.DataSource = res;
     
            if (editorElement.Items.Count > 0)
            {
                editorElement.SelectedValue = null;
                editorElement.SelectedValue = this.radGridView1.CurrentCell.Value;
     
            }
        }
     
        private DataTable GetData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Age", typeof(int));
            dt.Columns.Add("Date", typeof(DateTime));
            dt.Columns.Add("Bool", typeof(bool));
            for (int i = 0; i < 100; i++)
            {
                dt.Rows.Add("Name " + i, i, DateTime.Now.AddMinutes(i), i % 2 == 0 ? true : false);
            }
     
            return dt;
        }
     
        private IList<string> GetComboData(bool flag)
        {
            IList<string> data = new List<string>();
            if (flag)
            {
                data.Add("Mobile1");
                data.Add("Mobile2");
                data.Add("Mobile3");
                data.Add("Mobile4");   
            }
            else
            {
                data.Add("Fax1");
                data.Add("Fax2");
                data.Add("Fax3");
                data.Add("Fax4");
            }
             
            return data;
        }
    }

    I am also sending you a gif file showing the result on my end.

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top