当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默认选择第一行