0

I have added a DataGridViewComboBox to a bound DataGridView (grdBOOK), the DataGridViewComboBox will replace column 3 to allow for user selection. I'm struggling to set the default of the DataGridViewComboBox equal to the value of column 3 so user selection is not required if the value is correct.

I pulled the code below from the net, but I get an error:

DataGridViewComboBoxCell value is not valid.

I thought a ComboBox cell could be treated as a normal DataGridView cell, but (see code below) an error is generated when a string is added to the ComboBox column? I've trawled the net and SO for a few days but nothing works, any suggestions please?

    public void BOOK_COMBO2()       
        {
            DataGridViewComboBoxCell cb_cell = new DataGridViewComboBoxCell();
            DataGridViewComboBoxColumn cb_col = new DataGridViewComboBoxColumn();

            // Contract field
            cb_col.Items.AddRange("YEARLY", "MONTHLY", "");
            cb_col.FlatStyle = FlatStyle.Flat;
            cb_col.HeaderText = "newCONTRACT";
            cb_col.Width = 50;
            cb_col.ValueType = typeof(string);

            // Add ComboBox and test
            grdBOOK.Columns.Insert(5, cb_col);
            grdBOOK.Rows[14].Cells[4].Value = "zzz";        // No error adding string to normal dgv column
            grdBOOK.Rows[14].Cells[5].Value = "xxx";        // Error adding string to dgvcombobx column

            //copy old values to new combobox and set as default
            foreach (DataGridViewRow item in grdBOOK.Rows)
            {
                item.Cells[5].Value = item.Cells[3].Value;      
            }
            //hide original column
            grdBOOK.Columns[3].Visible = false;
        }
Zeus
  • 1,496
  • 2
  • 24
  • 53
  • Where are you calling this `BOOK_COMBO2` method from? – OhBeWise Apr 15 '16 at 15:03
  • Also, and more importantly, verify that the values in that column are always one of your options: `"YEARLY", "MONTHLY", ""`. You'll get that error if ever a value is not one of them; and yes, case and whitespace matters - `"Yearly"` and `" "` will throw the error. – OhBeWise Apr 15 '16 at 16:14
  • I call the method from the Main class and the datagridview was added to the main form at design time. I don't get any errors with the list of items, the error is caused by copying values from one of the datagridview columns to the datagridviewcombobox column. This line of code: ``item.Cells[5].Value = item.Cells[3].Value; `` – Zeus Apr 15 '16 at 21:30

2 Answers2

0

After more research on the net, IMHO using a ContextMenuStrip is a better method of achieving this. Link here. A ContextMenuStrip has better methods, events, properties etc. I hope this helps others looking for a solution.

Zeus
  • 1,496
  • 2
  • 24
  • 53
0
  private void dataGridView1_DataError(object sender,
            DataGridViewDataErrorEventArgs e)
        {
            // If the data source raises an exception when a cell value is 
            // commited, display an error message.
            if (e.Exception != null &&
                e.Context == DataGridViewDataErrorContexts.Commit)
            {
                MessageBox.Show("");
            }
        }




private void Form1_Load(object sender, EventArgs e)
    { dataGridView1.DataError +=
                dataGridView1_DataError;}