kvandenbosch 0 Newbie Poster

I have a datagridview with two columns: species and variety, where the dropdown list in variety depends on the species chosen in column 1. I have no problem setting the dropdown list for a new, blank record; the problem comes when I am populating the grid with existing records. I am using datagridview.datasource to populate my datagridview with a DataTable. When I run my application, I get the error “DataGridViewComboBoxCell value is not valid”. I know this is because at this point the dropdown list for that cell is blank, so the value of my Variety for that row is not in the dropdown list, therefore I get this error. How can I avoid this situation?

private void CreateDataGridView()
        {
            //Scientific name column
            DataGridViewComboBoxColumn scientificNameColumn = new DataGridViewComboBoxColumn();
            scientificNameColumn.HeaderText = "Scientific Name";
            scientificNameColumn.Width = 150;
            scientificNameColumn.AutoComplete = true;
            scientificNameColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
            scientificNameColumn.Resizable = DataGridViewTriState.True;
            
            scientificNameColumn.DataSource = varietyDataTable;
            scientificNameColumn.ValueMember = "SpeciesName";
            scientificNameColumn.DisplayMember = scientificNameColumn.ValueMember;

            scientificNameColumn.DataPropertyName = "ScientificName";

            //Variety column
            varietyColumn = new DataGridViewComboBoxColumn();
            varietyColumn.HeaderText = "Variety";
            varietyColumn.Width = 120;
            varietyColumn.AutoComplete = true;
            varietyColumn.Resizable = DataGridViewTriState.True;

            varietyColumn.DataPropertyName = "Variety";


            //Create datagridview           
            speciesDataGridView.Name = "speciesDataGridView";
            speciesDataGridView.Size = new Size(900,150);
            speciesDataGridView.Location = new Point(3, 3);
            speciesDataGridView.Dock = DockStyle.Top;
            speciesDataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            speciesDataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
            speciesDataGridView.EditMode = DataGridViewEditMode.EditOnKeystroke;
            
            speciesDataGridView.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.EnableResizing;
            speciesDataGridView.AllowDrop = false;
            speciesDataGridView.EditingControlShowing += speciesDataGridView_EditingControlShowing;
            speciesDataGridView.DataError += speciesDataGridView_DataError;

            speciesDataGridView.Columns.Add(scientificNameColumn);
            speciesDataGridView.Columns.Add(varietyColumn); 
            
//add grid to layout panel
            seedMixSpeciesLayoutPanel.Controls.Add(speciesDataGridView, 0, 1);

        }

private void speciesDataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            try
            {
                int index = speciesDataGridView.CurrentCell.ColumnIndex;
                
                comboBox = e.Control as ComboBox;

                if (speciesDataGridView != null)
                    //if Scientific Name combobox selection is changed
                    if (index == 0)
                    {
                        if (comboBox != null)
                        {
                            // Remove an existing event-handler, if present, to avoid 
                            // adding multiple handlers when the editing control is reused.
                            comboBox.SelectedIndexChanged -= ScientificName_SelectedIndexChanged;

                            // Add the event handler. 
                            comboBox.SelectedIndexChanged += ScientificName_SelectedIndexChanged;  
                        }
                    }
                if (index == 1)
                {
                    if (comboBox != null)
                    {
                        // Remove an existing event-handler, if present, to avoid 
                        // adding multiple handlers when the editing control is reused.
                        comboBox.Click -= new EventHandler(SetVarietyComboBox);

                        // Add the event handler. 
                        comboBox.Click += new EventHandler(SetVarietyComboBox);
                    }
                }
                combo.Validating -= HandleComboBoxValidating;
            }
            catch (Exception ex)
            {
                UiUtil.ShowMessage(ex);
            }
        }


void SetVarietyComboBox(object sender, EventArgs e)
        {
            DataGridViewComboBoxCell cell = speciesDataGridView.CurrentCell as DataGridViewComboBoxCell;
            cell.DataSource = varietyDataTable;
            cell.ValueMember = "VarietyName";
            cell.DisplayMember = cell.ValueMember;

            comboBox.DataSource = cell.DataSource;
            comboBox.ValueMember = cell.ValueMember;
            comboBox.DisplayMember = cell.DisplayMember;
        }

//called on load
public void SetSeedMixSpeciesDataSource(DataTable seedSpeciesDataTable)
        {
            try
            {
                speciesDataGridView.DataSource = seedSpeciesDataTable;
            }
            catch (Exception ex)
            {
                UiUtil.ShowMessage(ex);
            }
        }