Hello,
I have a hierarchical grid bound to a hierarchical data source. When I need to change the hierarchy, i modify the datasource then re-set the datasource property on the grid, but the problem is when i do this, the users current location gets reset, and all the bands collapse which is very annoying. Is there a better way to rebind the grid after the hierarchy changes? I tried Refresh, Rebind, but that doesn't work.
Thanks
Hi,
If you set (or re-set) the DataSource on the grid, all of the grid rows are destroyed and a completely new set of rows are created based on the new data source. So there is no way that the grid can maintain any kind of row or cell level state information, such as the ActiveRow, SelectedRows, or Expanded states of the rows.
The best thing to do is to avoid resetting the data source whenever possible.
What kind of DataSource are you using for the grid? And what kind of changes are you making to the structure of the data?
If you are using a DataSource that implements IBindingList, then you should not have to reset the grid's DataSource, because the DataSource will notify the grid of any changes in structure and the grid will update automatically.
I am using a BindingList. It does not update automatically. Do I need to do anything for it to update automatically?
I am using version 6.2.
I am using the BindingList<T> class. I implemented INotifyPropertyChanged for T and also changed the grid to bind to a BindingSource instead of binding directly to the DataSource.
Now, when the hierarchy changes, I see it reflected on the grid but not always. For instance, if I have 100 rows on the grid and I change the hierarchy so now there are 500 rows, you will only see the first 100 rows. I've tried calling Refresh, DataBind, Update, etc, but it does not seem to correctly paint the grid.
Is there a sample app with this working?
I'm getting a bit confused. Each time you reply to this thread, you seem to be reporting something completely different than the previous post. Are these all part of the same issue?
In any case, I'm not sure what you mean by "when the hierarchy changes". How, exactly, are you changing the hierarchy of your data in a BindingList<T>? That doesn't seem possible.
It sounds like maybe you mean you are removing some rows, but that has nothing to do with the hierarchy. It doesn't even have anything to do with INotifyPropertyChanged. The BindingList<T> itself will send notifications when rows are added or removed and the grid will update itself automatically.
If that's not working for you, then I haven't got any idea why that would be - unless maybe you are using multiple threads in your application and you are modifying the data source on a different thread so it's getting out of synch.
If even rebinding the grid doesn't fix it, then my guess is that your data source is not in the state that you think it's in, and that it only contains 100 rows.
What I mean by hierarchy changes is the hierarchy of the grid changes, similar to a re-grouping. My datasource is hierarchical, so a row can also contain a collection of rows. My issue is, when I change the hierarchy (ex: ungroup on the datasource), it does not correctly reflect on the grid unless I re-set the datasource. That is my big problem, that I need to re-set the datasource and the active row changes. I don't want to re-set the datasource.
After implementing INotifyPropertyChanged, the grid does not properly re-draw as I stated in the previous post. I know my datasource is in a correct state, because if I set the grid datasource to null, then set the datasource again, the rows will properly appear.
Caveno said:What I mean by hierarchy changes is the hierarchy of the grid changes, similar to a re-grouping. My datasource is hierarchical, so a row can also contain a collection of rows. My issue is, when I change the hierarchy (ex: ungroup on the datasource), it does not correctly reflect on the grid unless I re-set the datasource. That is my big problem, that I need to re-set the datasource and the active row changes. I don't want to re-set the datasource.
I'm still not following what this means.
I thought you wrote that you are using a BindingList<T>. I am not aware of any grouping functionality on BindingList<T>. And I am pretty certain that the BindingManager in DotNet does not support anything like that.
I don't see how you could possibly change the hierarchical structure of a BindingList,<T> at run-time, either. Whatever class T you are using cannot be changing the properties after it's compiled. Unless maybe you are implementing ITypedList yourself. And even if you are there in no way to notify the BindingManager that your ITypedList implementation has changed what it returns except to send a Reset notification.
Are you using some other kind of data source? Or are you manually modifying the BindingList<T> a row at a time?
Whatever you are doing to your data source, it's clear that the data source is not sending the property notifications to the grid (or that such notifications do not actually exist). In such a case, you can try calling grid.Rows.Refresh(ReloadData). That might work, depending on the extent of the changes you made to the data source. But if that doesn't work, then you will have to re-set the grid's DataSource.
The Refresh(ReloadData) did it!
Yes, I've implemented an ITypedList.
Thanks for your help!