DataGridViewComboBoxCell Binding - “value is not valid”

前端 未结 10 745
臣服心动
臣服心动 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:02

    Here's a complete example with a basic form and DataGridView added via the designer:

    Setup and bindings:

    private void Form1_Load(object sender, EventArgs e)
    {
    
        var colors = new List<Code>()
        {
            new Code() {Value= "R", Text = "Red"},
            new Code() {Value= "G", Text = "Green"},
            new Code() {Value= "B", Text = "Blue"}
        };
    
        var users = new List<User>()
        {
            new User() {Name = "Briana", FavoriteColor = "B"},
            new User() {Name = "Grace", FavoriteColor = "G"}
        };
    
        var colorCol = new DataGridViewComboBoxColumn();
        colorCol.DataSource = colors;
        colorCol.DisplayMember = "Text";
        colorCol.ValueMember = "Value";
        colorCol.DataPropertyName = "FavoriteColor";
    
        dataGridView1.Columns.Add(colorCol);
        dataGridView1.DataSource = users;
    
    }
    

    Some classes:

    public class Code
    {
        public string Value { get; set; }
        public string Text { get; set; }
    }
    
    public class User
    {
        public string Name { get; set; }
        public string FavoriteColor { get; set; }
    }
    
    0 讨论(0)
  • 2020-11-30 10:06

    I was having the same problem. The message was 100% spot on. The values for the combobox were like: Exact, StartsWith... and I was trying to set the value Exactă (not Exact). This was happening automatically as I was reading the DataTable for the DataGridView from an .xml file with DataTable.ReadXml(...). The values in the .xml file were off.

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

    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)
    
    0 讨论(0)
  • 2020-11-30 10:09

    I managed to find the solution not long after posting the question. For anyone else:

    The problem was that I was trying to assign the DataGridViewComboBoxCell.Value to an object, expecting that because the Cell was bound to a data source that it would automatically find the object in the source and update.

    This wasn't actually the case, you actually need to set the value equal to that of the ValueMember property for it to correctly update the value and binding. I believe I was using a property 'Name' for both ValueMember and DisplayMember (controls how the renders in the cell) so setting the Value to interface.ToString() (rather than the interface instance) works for the majority of cases. Then I catch and ignore any DataError exceptions that occur while I'm changing the source around.

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

    Set a null value to the cell:

    dataGridView.CurrentRow.Cells[NAME].Value = null;
    
    0 讨论(0)
  • 2020-11-30 10:17

    I am having the same problem. After populating my ComboBox column in the (unbouod) DataGrid, I solved my problem by setting the ValueMember property of the DataGridViewComboBoxColumn Apparently, just relying on the ToString() property of the objects in the ComboBox is not enough.

    Actual code:

    /// <summary>
    /// Populate the dataGridSplitVolumes data grid with all existing qualifications for the plan.
    /// </summary>
    /// <param name="bonus"></param>
    private void PopulateDataGridSplitVolumes(Bonus_Group bonus)
    {
      try
      {
        List<Qualification> qualifications = Qualification.Load(this.groupBonus.PlanID, this.ConnectionString);
        foreach (Qualification qual in qualifications)
        {
          DataGridViewComboBoxColumn col = (DataGridViewComboBoxColumn)this.dataGridSplitVolumes.Columns[0];
          col.Items.Add(qual);                    
        }
        SplitVolumeGrid_QualificationColumn.ValueMember = "Name";
      }
      catch (Exception ex)
      {
    #if DEBUG
        System.Diagnostics.Debugger.Break();
    #endif
        throw ex;
      }
    }//PopulateDataGridSplitVolumes     
    
    0 讨论(0)
提交回复
热议问题