Hi Experts,
I am new to Infragistics UltraGrid control. Here is my requirement. I have ClassA and ClassB as below:
class ClassA{ private string _propA1; private int _propA2; private string _propA3; private ClassB _classB;
public string PropA1 { get { return _propA1; } set { _propA1 = value; } } public int PropA2 { get { return _propA2; } set { _propA2 = value; } } public string PropA3 { get { return _propA3; } set { _propA3 = value; } } public ClassB ClassBProperty { get { return _classB; } set { _classB = value; } }}
class ClassB{ private string _propB1; private int _propB2; private string _propB3;
public string PropB1 { get { return _propB1; } set { _propB1 = value; } } public int PropB2 { get { return _propB2; } set { _propB2 = value; } } public string PropB3 { get { return _propB3; } set { _propB3 = value; } }}
Now i am binding a list of objects of ClassA to the UltraWinGrid as below:
private List<ClassA> _listA = null;
private void Form1_Load(object sender, EventArgs e){ _listA = new List<ClassA>();
for (int i = 0; i < 10; i++) { ClassA objA = new ClassA(); objA.PropA1 = "PropertyA1"; objA.PropA2 = i; objA.PropA3 = "PropertyA3"; objA.ClassBProperty = new ClassB(); objA.ClassBProperty.PropB1 = "PropertyB1"; objA.ClassBProperty.PropB2 = i + 10; objA.ClassBProperty.PropB3 = "PropertyB3"; _listA.Add(objA); }
this.ultraGrid1.DataSource = _listA;}
Below is how the grid displays. But, i want the drid to display all the propeties of ClassA and ClassB as columns in the grid. How can i achieve this?
Please let me know if you need more information. Attached is the code for reference.
Hi,
The WinGrid displays the data in the structure defined by you and the BindingManager in DotNet. So in this case, ClassBProperty is a single property of ClassA and so it shows the object as a single column. There's no way for the grid to break down ClassB into it's individual properties.
You could do this in a number of ways.
1) You could handle this yourself by hiding the ClassBProperty column in the grid and then adding an unbound column for each of ClassB's properties. You could have to handle a couple of grid events like InitializeRow to copy the data from the instance of ClassB into the appropriate unbound columns and then BeforeRowUpdate to reverse this process and copy any changes the user made back into the ClassB object (if the data is editable).
2) Another alternative would be to derive a class from List<ClassA>, and implement ITypeList. You could then override the GetProperties method and return a set of PropertyDescriptors that kow about the properties of ClassB and translate them into properties on ClassA. This implementation is a bit more complex and requires some advanced knowledge of PropertyDescriptors (you will need to create a custom PropertyDescriptor). But the advantage is that it is not specific to the WinGrid, it will work regardless of what control(s) you are binding to.