wpf devexpress gridcontrol捕获集合改变事件

本文介绍如何在GridControl的数据源发生变化时(如添加或移除项),通过监听ItemsSourceChanged事件并结合INotifyCollectionChanged接口来实现自动定位到表格的第一行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当gridcontrol的数据源itemsource改变时(添加一项或移除一项)通过下面方法可以捕获到该事件以进行相应处理


using System.Collections.Specialized;

        private void GridControl_ItemsSourceChanged(object sender, ItemsSourceChangedEventArgs e)
        {
            if (e.OldItemsSource is INotifyCollectionChanged)
                ((INotifyCollectionChanged)e.OldItemsSource).CollectionChanged -= Source_CollectionChanged;
            if (e.NewItemsSource is INotifyCollectionChanged)
                ((INotifyCollectionChanged)e.NewItemsSource).CollectionChanged += Source_CollectionChanged;
        }


        void Source_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                var tableView =gc.View as TableView;
                if (tableView != null)
                {
                    // tableView.MoveLastRow();
                    tableView.MoveFirstRow();
                }
            }
        }


Source_CollectionChanged事件会在每次添加或删除gridcontrol数据源中的item时触发

以上方法可以使gridcontrol默认选择第一行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值