Reputation: 2148
I have a List<SomeClass>
bound to DevExpressXtraGrid like:
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.
I've tried with MyXtraGrid.Refresh()
, tried to rebind with MyXtraGrid.DataSource = MyList
, but it didn't work.
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.
How to refresh the grid view to show object I've added ?
Upvotes: 2
Views: 25431
Reputation: 18290
Use the GridControl.RefreshDataSource Method as 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 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 IBindingList if you want it all to mesh together by itself. otherwise I do believe the RefreshDatasource should work.
Reference:
Refreshing Grid When Using Custom Enumerator
How to keep unchanged scroll position when refreshing grid data_
Filtering the Object DataSource
Upvotes: 1
Reputation: 9080
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 List
you 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.
Upvotes: 9