C# 在将项目添加到绑定到它的列表后刷新 DevExpress.XtraGrid
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9931964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Refreshing DevExpress.XtraGrid after adding items to list that was boun to it
提问by Dejo
I have a List<SomeClass>bound to DevExpressXtraGrid like:
我List<SomeClass>绑定到 DevExpressXtraGrid,例如:
MyXtraGrid.DataSource = MyList;
I have some columns made in XtraGrid designer. Everything is ok and rows were shown in grid, but when I add objects to MyList grid is not refreshed and new item was not shown.
我在 XtraGrid 设计器中制作了一些列。一切正常,行显示在网格中,但是当我将对象添加到 MyList 时,网格没有刷新,也没有显示新项目。
I've tried with MyXtraGrid.Refresh(), tried to rebind with MyXtraGrid.DataSource = MyList, but it didn't work.
我试过MyXtraGrid.Refresh(),试图重新绑定MyXtraGrid.DataSource = MyList,但没有用。
MyXtraGrix.MyView.PopulareColumns()is not an option, cause I don't wont all fields from MyList to be show in grid, and this will earse columns I've configured with designer.
MyXtraGrix.MyView.PopulareColumns()不是一个选项,因为我不会将 MyList 中的所有字段都显示在网格中,这将有助于我使用设计器配置的列。
How to refresh the grid view to show object I've added ?
如何刷新网格视图以显示我添加的对象?
回答by Odys
Simply do this:
只需这样做:
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();
}
You can also get the selected row and reselect it after the new datasource has been set.
您还可以获取所选行并在设置新数据源后重新选择它。
Also note that instead of Listyou can use BindingList<>in order to have the grid to update itself without you having to write a single line of code. Read more here.
另请注意,List您可以使用代替BindingList<>使网格自行更新,而无需编写一行代码。在这里阅读更多。
回答by Niranjan Singh
Use the GridControl.RefreshDataSource Methodas i am using with my collection data Source is List of some class and it contain list of another class to create master view details.
使用GridControl.RefreshDataSource 方法,因为我正在使用我的集合数据源是某个类的列表,它包含另一个类的列表以创建主视图详细信息。
GridControl scheduleGrid = sender as GridControl;
MyXtraGrid.DataSource = collection;
scheduleGrid.RefreshDataSource();
If you make changes to a IList (outside of the grid) I believe you would then have to call the RefreshDatasource method, and IList doesn't do change notifications. RefreshDataSource Method
I believe that you should inherit from IBindingListif you want it all to mesh together by itself. otherwise I do believe the RefreshDatasource should work.
如果您对 IList(网格外)进行更改,我相信您将不得不调用 RefreshDatasource 方法,并且 IList 不执行更改通知。RefreshDataSource 方法
我相信,如果您希望将所有内容单独组合在一起,您应该从IBindingList继承。否则我相信 RefreshDatasource 应该可以工作。
Reference:
Refreshing Grid When Using Custom Enumerator
How to keep unchanged scroll position when refreshing grid data_
Filtering the Object DataSource

