0

I have an issue wherein I have a DataGridViewComboBoxColumn in a DataGridView and I want to add an item to the DataSource.

I've initially set the DataSource property to a List<string> which works fine. Later I'll add an item to this list, which works fine. But when I try to choose this item in a combobox, I get a data validation error,

System.ArgumentException: DataGridViewComboBoxCell value is not valid.

Further, I can't actually set the combobox to the newly added value.

Here is a fully working example.

public partial class Form1 : Form
{
    List<string> Data { get; set; }

    public Form1()
    {
        InitializeComponent();

        // Populate our data source
        this.Data = new List<string> { "Thing1", "Thing2" };

        // Set up controls
        var gvData = new System.Windows.Forms.DataGridView();
        var col1 = new System.Windows.Forms.DataGridViewComboBoxColumn();
        var button = new System.Windows.Forms.Button();

        gvData.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { col1 });

        // Set the column's DataSource
        col1.DataSource = this.Data;
        col1.HeaderText = "Test";
        col1.Name = "col1";

        // Set up a button which adds something to the source
        button.Text = "Add";
        button.Location = new System.Drawing.Point(0, 200);
        button.Click += (e, s) => this.Data.Add("Thing3");

        this.Controls.Add(gvData);
        this.Controls.Add(button);
    }
}

How can I add items to the DataSource for my DataGridViewComboBoxColumn?

Kris Harper
  • 5,672
  • 8
  • 51
  • 96
  • Where are you doing / calling DataBind() Method..? I believe also that you need to assign the Combobox's `ValueMember and DisplayMember` just quickly taking a glance – MethodMan Apr 01 '13 at 21:06
  • @DJKRAZE I hadn't set them because I read that for `string` values it shouldn't matter. I will try now. I don't know about `DataBind`. I have never used that method when setting a data source in WinForms. – Kris Harper Apr 01 '13 at 21:11
  • DataBind() normally used in DataGridview but I was just noticing some other things in regards to the actual combobox that's why I mentioned it – MethodMan Apr 01 '13 at 21:18
  • The DataGridView doesn't seem to have a `DataBind` method. I looked into `ValueMember` and `DisplayMember`, but I don't know how to set them. They are supposed to be properties of my objects, but my objects are just strings. – Kris Harper Apr 01 '13 at 21:20
  • 1
    @DJKRAZE [DataBind](http://msdn.microsoft.com/en-us/library/system.web.ui.control.databind.aspx) is for ASP.Net – Jacob Seleznev Apr 01 '13 at 21:22
  • @KrisHarper Your code works fine on my machine. What is not working? – Jacob Seleznev Apr 01 '13 at 21:23
  • `Jacob` that's correct I just realized he's doing winforms sorry – MethodMan Apr 01 '13 at 21:25
  • @JacobSeleznev [Here is a screenshot](http://i.imgur.com/RwsSo9Q.png) of the error that pops up. The exact steps to reproduce are 1. Set a value in the first row of the grid. Make sure you are no longer in edit mode. 2. Click the button to add a value. 3. Attempt to change the value previously set. I immediately get this error. – Kris Harper Apr 01 '13 at 22:52

1 Answers1

1

Changing

button.Click += (e, s) => this.Data.Add("Thing3");

to

           button.Click += (e, s) =>
           {
                col1.DataSource = null;
                this.Data.Add("Thing3");
                col1.DataSource = Data;
           };

has worked for me.

Jacob Seleznev
  • 8,013
  • 3
  • 24
  • 34
  • 1
    While this gets rid of the message, it has a number of other side effects. Most importantly, setting the `DataSource` to `null` clears the values of every row currently in the grid. Secondly, the add button now adds a second column with heading "Length." Thirdly, the grid now appears to be buggy. Choosing "Thing3" more than once results in an NRE. – Kris Harper Apr 02 '13 at 12:40
  • You are setting DataSource of **col1**, right? It shouldn't affect every datagrid's row. – Jacob Seleznev Apr 02 '13 at 22:36
  • Kris, i did not experience any of the side-effects you mentioned. I just took your test code from the question body, and replaced the Click handler as Jacob suggested... And it worked an intended, no lost values, no second column, and no NREs... – C.B. Apr 21 '13 at 08:20