Question

Project: Winforms, .NET framework: 3.5 (Visual Studio 2008)

My problem is:

I have a DataGridView with several columns, some of the type and another type DataGridViewTextBoxColumn, DataGridViewComboBoxColumn, DataGridViewComboBoxColumn column is filled by an entity supplied by the Entity Framework.

// Example
((System.ComponentModel.ISupportInitialize)(this.EntityBindingSource)).BeginInit();
this.EntityBindingSource.DataSource = this.dtContext.ExampleEntity;

this.ComboColumn.DataSource = this.EntityBindingSource;
this.ComboColumn.DataPropertyName = "ExampleId";
this.ComboColumn.DisplayMember = "Example";
this.ComboColumn.ValueMember = "ExampleId";

When you run and load the form, fine

But the problem comes when at runtime I want to select an item from the collection of DataGridViewComboBoxColumn

That in the event CellEndEdit

private void dgvDetalle_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
   DataGridViewCell cell = dgvDetalle.CurrentCell;
   DataGridViewComboBoxCell comboCell = (DataGridViewComboBoxCell)this.dgvDetalle.CurrentRow.Cells["ComboColumn"];

   // Attempt 1
   comboCell.Value = (ExampleEntity)comboTarifaImpuesto.Items[0];

   // Attempt 2
   comboCell.Value = ObjetoEntity;

   // Attempt 3
   comboCell.Value = "ValueExample"

   // Attempt 4
   comboCell.Value = ObjetoEntity.ToString();
}
  • Try selecting one item from combobox
  • Try selecting an object of the entity
  • Try selecting by means of a chain or Display Member Value

and I can not select the desired items, I get the exception:

DataGridViewComboBoxCell value is invalid

Was it helpful?

Solution

thanks to assistance provided in another question: DataGridViewComboBoxCell Binding - "value is not valid"

I could fix it, tube to re-enter the following properties of DataGridViewComboBoxCell:

comboCell.DisplayMember="Example";
comboCell.ValueMember="ExampleId";

as I had specified in the type column DataGridViewComboBoxColumn I already worked with this property correctly. Value

comboCell.Value = ObjetoEntity.ExampleId.ToString();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top