1

我有一个方法,更新包含类型DataGridViewComboBoxCell的列,早期ComboBoxCell为空,选择产品和ComboBoxCell更新时添加新记录做得很好,但当我修改它发送异常: “ DataGridViewComboBoxCell值无效“ 如果您在重新分配DataSource属性时不适用。“DataGridViewComboBoxCell值无效”。属性DataSource

这里的方法:

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没有空值,该引用以及

非常感谢你的帮助

回答

2

我已经尝试使用BindingList,并得到了相同的异常 porfin可以解决它。

返回DataSource属性来指定在索引未命中找不到的项目,在那里为了避免这个论坛只指定DataGridView有“DataError”的事件,并留下你空的,这真的工作,但是不好看。

如何解决这是这种简单的方法。 (用绳子空)

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; 
} 

非常感谢您的帮助(IBC)

0

转到这里http://msdn.microsoft.com/en-us/library/ms132679.aspx

这是一个BindingList。尝试将您的combocolumn数据放入绑定列表中,然后将combocolumn的数据源设置为绑定列表。当您需要更改组合框中的内容时,请不要将列的数据源设置为不同的bindingList实例,而是尝试清除原始绑定列表中的所有项目,然后逐个添加新项目。当绑定列表的listChanged事件触发时,datagridviewcombobox应该更新。

当绑定列表包含很多项目时,这可能会有点麻烦。你可能想从中赚取的BindingList继承一个新的类,并把这个在它:

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

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

     this.ResetBindings(); 
    } 

这样可以防止ListChanged事件被解雇每次添加一个项目的时间。 ResetBindings似乎与触发listchanged具有相同的效果。

可能有更好的解决方案来解决这个问题,但是这在过去对我有用。