DataGridView error: System.FormatException: DataGridViewComboBoxCell value is not valid RRS feed

  • Question

  • I receive this error during run-time when the DataGridView is trying to display the data from a data source: "System.FormatException: DataGridViewComboBoxCell value is not valid"

    My "binding" code is shown below:

    Code Snippet

    DataSet dsCustomer;
    DataSet dsComboBoxData;

    BindingSource m_ProspectBindingSource = new BindingSource();

    dgvProspects.DataSource = m_ProspectBindingSource;
    m_ProspectBindingSource.DataSource = dsCustomer.Tables["prospects"];

    DataGridViewComboBoxColumn dgvCol = (DataGridViewComboBoxColumn)dgvProspects.Columns["colProspectStatus"];
    dgvCol.DataSource = dsComboBoxData.Tables["prospect_statuses"];                               
    dgvCol.ValueMember = dsComboBoxData.Tables["prospect_statuses"].Columns["status_id"].ToString();
    dgvCol.DisplayMember = dsComboBoxData.Tables["prospect_statuses"].Columns["description"].ToString();

    Note shown is code that fills the dsCustomer and dsComboBoxData DataSets via stored procedures from SQL Server 2005.

    Columns from the SQL Server database:
    prospect_status table:
        status_id is a smallint
        description is varchar(40)

    prospect table:
        prospect_item_status is a smallint

    The "prospects" data in dsCustomer appears to be correct in the visualizer, as well as the "prospect_statuses" in dsComboBoxData.

    As a side note, if I change the column "colProspectStatus" from a ComboBoxColumn to a TextBoxColumn, I don't get any errors.

    Thanks in advance for any help.
    Tuesday, September 25, 2007 7:43 PM



    Hi VBCoder13,

    Your code has no problem, but it’s better to set the DisplayMember and ValueMember before you set the DataSource. And this error occurs when the prospect_item_status has one or more value that cannot be found in the prospect_status table. I suggest you check your database to make sure all records in the prospect_item_status should be contained in the prospect_status table. Creating a Foreign Key between the two tables is a good choice.

    Hope this helps.
    Best regards.
    Rong-Chun Zhang

    Thursday, September 27, 2007 6:45 AM