1

我有一个更新包含类型 DataGridViewComboBoxCell 的列的方法,早期 ComboBoxCell 为空,选择产品并在添加新记录时更新 ComboBoxCell 做得很好,但是当我修改它时会发送异常:“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 没有空值,引用很好

非常感谢您的帮助

4

2 回答 2

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)

于 2011-11-04T14:52:02.070 回答
0

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

这是一个绑定列表。尝试将您的组合列数据放入绑定列表,然后将组合列的数据源设置为绑定列表。当您需要更改组合框中的内容时,不要将列的数据源设置为不同的 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 具有相同的效果。

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

于 2011-11-04T06:51:27.867 回答