views:

6066

answers:

3

I'm trying to bind separate ComboBox cells within a DataGridView to a custom class, and keep getting a "DataGridViewComboBoxCell value is not valid" error.

I'm currently assigning the data source for the cell to an IList from a Dictionary I've got. Upon setting the data source however, the index for the ComboBoxCell isn't set, so it has an invalid value selected.

I'm trying to figure out how to get it to select a real value, e.g. the 0th item within the list it has been given to remove this error, or find another way to solve the problem. Anyone have any suggestions?

Thanks

+3  A: 

It appears as usual I find the answer just as I give up trying and make a post. I didn't realise that the DataGridViewComboBoxCell.Value needed to correspond to the .DisplayMember property... and not the underlying selected object itself.

Therefore setting it to my custom interface.ToString() etc works for the majority of cases. Then I catch and ignore any DataError exceptions that occur while I'm changing the source around.

Ian
A: 

Afters hours of trials, I finally found a solution that works.

// Create a DataGridView
System.Windows.Forms.DataGridView dgvCombo = new System.Windows.Forms.DataGridView();

// Create a DataGridViewComboBoxColumn
System.Windows.Forms.DataGridViewComboBoxColumn colCombo = new 

System.Windows.Forms.DataGridViewComboBoxColumn();

// Add the DataGridViewComboBoxColumn to the DataGridView
dgvCombo.Columns.Add(colCombo);

// Define a data source somewhere, for instance:
public enum DataEnum
{
    One,
    Two,
    Three
}

// Bind the DataGridViewComboBoxColumn to the data source, for instance:
colCombo.DataSource = Enum.GetNames(typeof(DataEnum));

// Create a DataGridViewRow:
DataGridViewRow row = new DataGridViewRow();

// Create a DataGridViewComboBoxCell:
DataGridViewComboBoxCell cellCombo = new DataGridViewComboBoxCell();

// Bind the DataGridViewComboBoxCell to the same data source as the DataGridViewComboBoxColumn:
cellCombo.DataSource = Enum.GetNames(typeof(DataEnum));

// Set the Value of the DataGridViewComboBoxCell to one of the values in the data source, for instance:
cellCombo.Value = "Two";
// (No need to set values for DisplayMember or ValueMember.)

// Add the DataGridViewComboBoxCell to the DataGridViewRow:
row.Cells.Add(cellCombo);

// Add the DataGridViewRow to the DataGridView:
dgvCombo.Rows.Add(row);

// To avoid all the annoying error messages, handle the DataError event of the DataGridView:
dgvCombo.DataError += new DataGridViewDataErrorEventHandler(dgvCombo_DataError);

void dgvCombo_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
    // (No need to write anything in here)
}

That is all.

A: 

I had the same problem.

In my case the solution was to fill the data adapter of the Foreign key table. This was not being automatically filled and this was the cause of the problem.

In the Page_Load Event:

Me.TblUserTypesTableAdapter.Fill(Me.DonateDataSet.tblUserTypes)

Regards Mark Jarzebowski

Mark Jarzebowski