DataGridViewComboBoxCell Binding - “value is not valid”

前端 未结 10 754
臣服心动
臣服心动 2020-11-30 09:45

I\'m trying to bind separate ComboBox cells within a DataGridView to a custom class, and keep getting an error

DataGridViewComboBoxCell value is not v

相关标签:
10条回答
  • 2020-11-30 10:19

    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.

    0 讨论(0)
  • 2020-11-30 10:21

    For the sake of people not struggling as much as i did.

    When binding the combo you are setting a DisplayMember (what the user will see) and ValueMember (what your application will get).

    After setting up these you need to set up the Value and this is where it fails. Basically the TYPE of the value needs to be the same TYPE as the ValueMember.

    So if your value member is an ID obviously its of type INT and you need to set your value to int for example Cell.Value = 1;.

    0 讨论(0)
  • 2020-11-30 10:23

    Here's my simple solution when using enums

    ColumnType.ValueType = typeof (MyEnum);
    ColumnType.DataSource = Enum.GetValues(typeof (MyEnum));
    

    you can do that just after "InitializeComponent();"

    0 讨论(0)
  • 2020-11-30 10:26

    use DataError Event handler,

    private void shahriartableDataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
            {
                //You don't have to write anything here !
            }
    

    and your DisplayMember and ValueMember data type should be the same, in my case I have CountryName for both. Everything works fine for me...!!

    0 讨论(0)
提交回复
热议问题