Hi,
I have a class like the following
class MyCompositeClass { private string property1 = "prop1"; [DisplayName("Identifier")] public string Property1 { get { return property1; } set { property1 = value; } } private string property2 = "Prop2"; public string Property2 { get { return property2; } set { property2 = value; } } private MyInnerClass innerClassObj = new MyInnerClass(); public MyInnerClass InnerClassObj { get { return innerClassObj; } set { innerClassObj = value; } } }
class MyInnerClass { public MyInnerClass() { mystring = "sadf"; } private string mystring = null; public string Mystring { get { return mystring; } set { mystring = value; } } }
i am binding a list of MyCompositeClass to the ultragrid like following
BindingList<MyCompositeClass> comp = new BindingList<MyCompositeClass> { new MyCompositeClass() ,new MyCompositeClass()}; ultraGrid1.DataSource = comp;
Can I show the public properties of MyInnerClass in the same band as the MyCompositeClass in some straightforward way?
Okay, that can be done. It's not as simple as just applying an attribute, though. You have to implement the ITypedList interface and create PropertyDescriptors that know how to deal with the child properties as if they were properties on the parent object.
I have attached a sample of this technique here. I wrote this sample many years ago, so I'm a little rusty on the details, but let me know if you have any questions and I will try to help as best I can.
We have multiple properties inside the inner class. Ideally we would have liked to specify some sort of attribute which would have allowed it to be displayed in the same band. The suggested ways will have to be implemented in all the grids where the object is binded which makes it tightly coupled and adding any new property itself will need additional editing everywhere.
There are a number of approaches you can take here.
Do you need the MyString field to be editable?
Is there really only one string property on MyInnerClass, or is that just an example?
If you have only a single property to display and it doesn't require editing, then this is very simple. All you have to do is override ToString on MyInnerClass and return MyString. the grid will show the ToString method of the property value.
If you need the string to be editable, it's a little more complex. You could achieve that using a DataFilter.
If there is more than one property on MyInnerClass, then you could add an unbound column to the grid for each one and then use the InitializeRow event to populate the unbound columns with the data from MyInnerClass on each row. You would also have to handle BeforeRowUpdate to write any edits to the underlying object if the data is editable.
Hello ,
If your inner class was a collection then UltraGrid will consider it as a child band. Since your MyInnerClass is not a collection then UltraGrid created column (single column ), for that field and uses ToString(); method in order to display value for the cells of that column. So if you want to display public properties of the MyInnerClass in the same band as MyCompositClass, you could use Reflection in order to get all public properties of your MyInnerClass and to create unbound column for each property and then to use InitiaLizeRow event of UltraGrid in order to fill the unbound columns cells with data.
Please let me know if you have any further questions.