locked
DataGridViewComboBoxColumn with datasource binds to generic list RRS feed

  • Question

  • Hi,
    I'm trying to bind the DataGridViewComboBoxColumn to a generic list holding business entities.
    I bind the value to the entities Id (guid) and the text to the entities name (string)
    When I click on the combobox I can see all entities names but when selecting an item I recieve an error "Selected value is not valid" and the combobox displays the selected entity Id.
    I've seen some posts about similar cases but couldn't find any helpful information.

    Id
    Monday, September 14, 2009 8:37 PM

Answers

  • Hi,

    DataGridViewComboBoxColumn does support Guid type as ValueMember. But the underline datasource should declear that column as Guid type. Otherwise you will get error during binding process. Please look at the following sample.
    public partial class Form1 : Form
        {
            private List<MyObject> lstObj = new List<MyObject>();
            private DataTable dtSource = new DataTable();

            public Form1()
            {
                InitializeComponent();

                dtSource.Columns.Add("Column1");
                dtSource.Columns[0].DataType = typeof(Guid);
                Guid gu1 = Guid.NewGuid();
                Guid gu2 = Guid.NewGuid();
                Guid gu3 = Guid.NewGuid();
                dtSource.Rows.Add(gu1);
                dtSource.Rows.Add(gu2);
                dtSource.Rows.Add(gu3);

                lstObj.Add(new MyObject(gu1, "Item1"));
                lstObj.Add(new MyObject(gu2, "Item2"));
                lstObj.Add(new MyObject(gu3, "Item3"));

                DataGridViewComboBoxColumn dgvCmb = new DataGridViewComboBoxColumn();
                dgvCmb.DataSource = lstObj;
                dgvCmb.DisplayMember = "Name";
                dgvCmb.ValueMember = "ID";
                dgvCmb.DataPropertyName = "Column1";
                dataGridView1.Columns.Add(dgvCmb);

                dataGridView1.DataSource = dtSource;
            }
        }

        public class MyObject
        {
            private Guid id;
            public Guid ID
            {
                get { return id; }
                set { id = value; }
            }

            private string name;
            public string Name
            {
                get { return name; }
                set { name = value; }
            }

            public MyObject(Guid id, string name)
            {
                this.id = id;
                this.name = name;
            }
        }

    From the above sample you can see that ID property is a Guid type. I have declear the DataType of  Column1 of dtSource as Guid type. The code can be run properly. Another information that should be noticed is if you add a new Guid to the Column1 which doesn't contain in the lstObj, you will get error "System.ArgumentException: DataGridViewComboBoxCell value is not valid". So please make sure all the value of the cells of that ComboBoxColumn should be the value in the lstObj.

    If you have any further question, please feel free to tell me.

    Sincerely,
    Kira Qian
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    • Proposed as answer by Andrés N. García Thursday, September 17, 2009 2:15 PM
    • Marked as answer by Kira Qian Monday, September 21, 2009 8:19 AM
    Thursday, September 17, 2009 8:13 AM