You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

BindingList绑定DevExpress GridView后修改值无法实时更新问题

问题:DevExpress GridView修改值后界面无法实时更新相关数据

我创建了实现INotifyPropertyChanged和INotifyPropertyChanging接口的自定义类PrecioServiciosSegurosModelo,并将其BindingList数据源绑定到DevExpress GridView。目前添加列表元素时界面能实时显示,但修改值并在CellValueChanging事件中执行EditMontos操作后,界面无法实时更新相关数据。


自定义类代码

public class PrecioServiciosSegurosModelo : INotifyPropertyChanged, INotifyPropertyChanging
{
    private int _id;
    private int _coberturaID;
    private int _servicioID;
    private int _seguroID;
    private decimal _preBase;
    private decimal _precioFinal;
    private decimal _montoSeguro;
    private decimal _adicional;
    private int _nuevo;
    private DB.Coberturas _coberturas;
    private DB.Seguros _seguros;
    private DB.Servicios _servicios;

    public int ID
    {
        get { return _id; }
        set { SetProperty(ref _id, value); }
    }

    public int CoberturaID
    {
        get { return _coberturaID; }
        set { SetProperty(ref _coberturaID, value); }
    }

    public int ServicioID
    {
        get { return _servicioID; }
        set { SetProperty(ref _servicioID, value); }
    }

    public int SeguroID
    {
        get { return _seguroID; }
        set { SetProperty(ref _seguroID, value); }
    }

    public decimal PreBase
    {
        get { return _preBase; }
        set { SetProperty(ref _preBase, value); }
    }

    public decimal PrecioFinal
    {
        get { return _precioFinal; }
        set { SetProperty(ref _precioFinal, value); }
    }

    public decimal MontoSeguro
    {
        get { return _montoSeguro; }
        set { SetProperty(ref _montoSeguro, value); }
    }

    public decimal Adicional
    {
        get { return _adicional; }
        set { SetProperty(ref _adicional, value); }
    }
    
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangingEventHandler PropertyChanging;

    protected virtual void OnPropertyChanging([CallerMemberName] string propertyName = null)
    {
        PropertyChanging?.Invoke(this, new PropertyChangingEventArgs(propertyName));
    }

    private bool SetProperty<T>(ref T backingField, T value, [CallerMemberName] string propertyName = null)
    {
        if (EqualityComparer<T>.Default.Equals(backingField, value))
        {
            return false;
        }

        OnPropertyChanging(propertyName);
        backingField = value;
        OnPropertyChanged(propertyName);
        return true;
    }
}

BindingList转换与操作类代码

public class preseguroservicios
{
    public static BindingList<PrecioServiciosSegurosModelo> Items = new BindingList<PrecioServiciosSegurosModelo>();

    public static void ToBindinglist(ServicioModelo objeto)
    {
        Items = new BindingList<PrecioServiciosSegurosModelo>(objeto.precioServicioSeguro.ToList());
    }
    
    public static void EditMontos(int Row)
    {
        //Items[Row].PrecioFinal = Items[Row].Pre_Base - (Items[Row].Pre_Base * Items[Row].Coberturas.PorcentajeCobertura / 100) + Items[Row].Adicional;
        Items[Row].MontoSeguro = (Items[Row].PreBase * Items[Row].Coberturas.PorcentajeCobertura / 100);
        //  Items[Row].PrecioFinal = Cant * Presentaciones[Row].Precio_Unidad;
    } 
}

GridView事件代码

private void viewcoberturas_CellValueChanging_1(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
{
    preseguroservicios.EditMontos(e.RowHandle);
}

问题解决方法

  1. 更换事件触发时机:
    将CellValueChanging事件替换为CellValueChanged事件,前者在单元格值未提交到数据源时触发,此时修改属性可能无法被GridView及时捕获;后者在值提交后触发,执行EditMontos的时机更准确。

  2. 确保关联属性的变更通知:
    检查DB.Coberturas类是否实现了INotifyPropertyChanged接口,如果PorcentajeCobertura属性变更时未触发PropertyChanged事件,计算MontoSeguro时的依赖值变更无法被感知,导致界面不更新。

  3. 强制GridView刷新:
    在EditMontos方法末尾添加GridView的刷新代码,确保界面同步:

    public static void EditMontos(int Row, DevExpress.XtraGrid.Views.Grid.GridView view)
    {
        Items[Row].MontoSeguro = (Items[Row].PreBase * Items[Row].Coberturas.PorcentajeCobertura / 100);
        // 刷新指定行
        view.RefreshRow(Row);
    }
    

    调用时传入GridView实例:

    preseguroservicios.EditMontos(e.RowHandle, viewcoberturas);
    
  4. 验证数据源一致性:
    调用ToBindinglist重新赋值Items后,确保GridView的DataSource也同步设置为新的Items实例,避免数据源绑定不一致导致的更新失效。

内容的提问来源于stack exchange,提问作者Yisus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 00:22:50