How to display objects with dynamic fields in wpf data grid?

I would do this to my interface public interface IMyObject { IEnumerable GetFieldNames(); IEnumerable GetFieldTypes(); //i would add this property, then you can bind directly to it. //basically it is a collection indexer, indexed by string object thisString name { get; set; } object GetField(string name); void SetField(string name, object value); } I would build the columns in code like so, (where stringKeyCollection is a collection of strings returned from GetFieldNames() - although personally I would keep this information separate from my object - like a master definition) foreach(String item in stringKeyCollection){ //create the base column (use whatever column type you want DataGridBoundColumn column = new DataGridBoundColumn(); //create the binding for the column column. Binding = new Binding("" + item + ""); //set the header column.

Header = item; } then you have objects in each cell of the grid and you can define templates however you wish.

I was not aware of the possibility to bind to the indexer of an object. I think this solution suits best my needs. – Oliver Hanappi Mar 25 '10 at 7:22.

The fact that the type of your bound objects changes each time is not hugely important, if your grid is set to AutoGenerate columns then it will create the columns for you. However this could lead to some rather un-pretty results. What I would suggest is this: with your data objects, annotate each displayable property with a custom attribute, this is simply to mark it for later inspection once you have obtained your collection of items, take the first item in the list and pass it to a factory function that returns grid columns the grid column factory function can inspect the data object using reflection looking for properties with the special attribute you used earlier, and create the appropriate grid column with the appropriate binding and value converter add the collection of grid columns to the grid, and bind the data this approach depends upon all the items in the collection being of the same type, but should be reasonably snappy.

If you have different items in the collection and they have little or no commonality then you could look to the method where you query each item for it's bindable properties and then mash the whole lot together.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions