2012-03-29 118 views
2

我有一个List<SomeClass>势必DevExpressXtraGrid,如:刷新DevExpress.XtraGrid将项目添加到列表中后,奔它

MyXtraGrid.DataSource = MyList; 

我在XtraGrid中的设计师做了一些列。一切正常,行显示在网格中,但是当我添加对象到MyList网格不刷新,并且新项目不显示。

我试过MyXtraGrid.Refresh(),试图用MyXtraGrid.DataSource = MyList重新绑定,但它没有奏效。

MyXtraGrix.MyView.PopulareColumns()是不是一个选项,因为我不会从MyList的所有字段都显示在网格中,这将消除我已与设计师配置的列。

如何刷新网格视图以显示已添加的对象?

回答

8

只要做到这一点:

private void BindCollection(IEnumerable collection) 
    { 
     // keep current index 
     GridView view = MyXtraGrid.Views[0] as GridView; 
     int index = 0; 
     int topVisibleIndex = 0; 
     if (view != null) 
     { 
      index = view.FocusedRowHandle; 
      topVisibleIndex = view.TopRowIndex; 
     } 

     MyXtraGrid.BeginUpdate(); 
     MyXtraGrid.DataSource = collection; 
     MyXtraGrid.RefreshDataSource(); 

     if (view != null) 
     { 
      view.FocusedRowHandle = index; 
      view.TopRowIndex = topVisibleIndex; 
     } 

     MyXtraGrid.EndUpdate(); 
    } 

您也可以选择行,然后重新选择新的数据源已定之后。

另请注意,您可以使用BindingList<>而不是List来更新网格,而无需编写一行代码。阅读更多here

1

使用GridControl.RefreshDataSource Method,因为我使用的是我的收集数据源是某个类的列表,它包含另一个类的列表以创建主视图详细信息。

GridControl scheduleGrid = sender as GridControl; 
MyXtraGrid.DataSource = collection; 
scheduleGrid.RefreshDataSource(); 

如果你更改了IList的(电网外)我相信你 将不得不调用RefreshDatasource方法,以及IList的 没有做更改通知。 RefreshDataSource Method

我相信你应该从IBindingList继承,如果你想 这一切都自己网格一起。否则我确信RefreshDatasource应该可以工作。

参考:
Refreshing Grid When Using Custom Enumerator
How to keep unchanged scroll position when refreshing grid data_
Filtering the Object DataSource