Question

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

Was it helpful?

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)

OTHER TIPS

Go here http://msdn.microsoft.com/en-us/library/ms132679.aspx.

This is a BindingList. Try putting your combocolumn data into a binding list and then setting the datasource of the combocolumn to the bindinglist. When you need to change what's in the combobox, instead of setting the datasource of the column to a different bindingList instance, try clearing all the items of your original binding list and adding the new ones in there one by one. When the listChanged event of the binding list fires, the datagridviewcombobox should update.

This can be kind of troublesome when the bindinglist contains a lot of items. You might want to make a new class that inherits from bindinglist and put this in it:

public void clearAndAddList(List<T> newData)
    {
        this.Clear();

        this.RaiseListChangedEvents = false;
        foreach (var item in newData)
            this.Add(item);
        this.RaiseListChangedEvents = true;

        this.ResetBindings();
    }

This prevents a listchanged event from being fired every time you add an item. ResetBindings seems to have the same effect as firing listchanged.

There's probably a better solution to this problem, but this has worked for me in the past.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top