I have a class
public class MyObject {
public string Caption { get; set; }
public string SomeText { get; set; }
public IList<MyObject> ChildItems { get; set; }
public Dictionary<string,Action> ItemActions { get; set; }
}
and an UltraGrid whose DataSource will be an IList<MyObject>.
IList<MyObject> list = ListRepository.GetAll<MyObject>();
MyUltraGrid.DataSource = list.
When I set an IList<MyObject> as the UltraGrid's DataSource it seems that the Dictionary property ItemActions is being chosen as the property to use to create bands in the grid and I get Key Not Found exceptions when trying to access columns in lower bands, because I'm expecting the columns in lower bands to be keyed of of other properties of MyObject.
For example:
MyGrid.DisplayLayout.Bands[1].Columns["SomeText"].PerformAutoResize();
would result in an exception because the "SomeText" key of the Columns collection doesn't exist. Band[1] gets created from the Dictionary property of MyObject instead of the IList<MyObject> property.
I have worked around this for now by changing the ItemActions property to type Object instead of a Dictionary and casting when I need to reference it, but I'd rather not have to do that.
Is there a way to force the UltraGrid binding to choose ChildItems as the hierarchy path?
Hi Shawn,
If you are binding the grid to a recursive data source (which you are) and you only care about 3 levels of data, then it would be a good idea to set the MaxBandDepth property to 3. That will get rid of all those extra bands and you won't have to worry about coding your loops to ignore them.
It will also give you a boost to performance.
Ah, what another day will do for your code. It turns out that it was indeed not a problem with the properties and binding, but rather the band level that I was inspecting. I had a foreach loop that looped through the entire bands collection and the exception was being thrown on level 100. I only have 3 levels that I'm interested in so I rewrote the loop and all is well using a Dictionary property as I originally intended.
I did, however, take your advice and switch over to BindingList instead of IList. Thanks for the nudge in that direction!
Hi,
It's probably because you are using IList<T>. Using an interface instead of a concrete implementation will cause all sorts of problems when binding.Also, IList is not a great interface to use for DataBinding, anyway. It's very limited and won't support all of the notifications needed for DataBinding.
Change IList<T> to List<T> or better yet, BindingList<T> and it will work a lot better.