Search code examples
c#winformsentity-frameworkdatagridviewdatagridviewcombobox

"DataGridViewComboBoxCell value is not valid." Property DataSource


I have a method that updates a column that contains a type DataGridViewComboBoxCell, early ComboBoxCell is empty, select a product and ComboBoxCell is updated when adding a new record is done well, but when I modify it sends an exception : "DataGridViewComboBoxCell value is not valid" Not if you are when you reassign the DataSource property.

Here the method:

private void CargarTipoGasto(ref DataGridViewComboBoxCell ComboColumn)
{
   ComboColumn.DataSource = from oPro in dtContext.tblProducto
                            where oPro.ProductoId == objProducto.ProductoId
                            from oMat in dtContext.tblMatrizDeCuentasGD
                            where oMat.Partida.Substring(0,3) ==
                              oPro.tblObjetoGasto.ObjetoGastoId.Substring(0,3)
                            from oTipGas in dtContext.tblTipoGasto
                            where oMat.TipoGasto == oTipGas.TipoGastoId
                            select oTipGas;

   ComboColumn.ValueMember = TIPOGASTO_ID;
   ComboColumn.DisplayMember = TIPOGASTO_VALOR;
}

verique that no null values, that the references are well

thank you very much for any help


Solution

  • I had already tried to do with the BindingList and got the same exception porfin could solve it.

    Returning DataSource property to specify the item to select not found in index misses, out there in that to avoid this forum only specify the event that there is "DataError" to DataGridView and leave you with empty and this really works but is not well seen .

    how to solve it was this simple way. (with string empty)

    private void CargarTipoGasto(ref DataGridViewComboBoxCell ComboColumn)
    {
       ComboColumn.Value = string.Empty;
       ComboColumn.DataSource = from oPro in dtContext.tblProducto
                                where oPro.ProductoId == objProducto.ProductoId
                                from oMat in dtContext.tblMatrizDeCuentasGD
                                where oMat.Partida.Substring(0,3) ==
                                  oPro.tblObjetoGasto.ObjetoGastoId.Substring(0,3)
                                from oTipGas in dtContext.tblTipoGasto
                                where oMat.TipoGasto == oTipGas.TipoGastoId
                                select oTipGas;
    
       ComboColumn.ValueMember = TIPOGASTO_ID;
       ComboColumn.DisplayMember = TIPOGASTO_VALOR;
    }
    

    Thank you very much for the help (IBC)