In ASP.NET, binding a DataGrid to a list of objects is super-easy.
I end up with a row for each object in the list, and any cell in a given row is bound to a property of the corresponding object.
However, suppose one of the properties of my object is a dictionary, and each is expected to contain a specific key. Is there any way to bind one of my DataGridColumns to that dictionary key?
Thanks.
1:
If you add a handler for DataGrid.ItemDataBound, you can explicitly set a column value from your Dictionary. The very simplest example:
protected void OnItemDataBound(object sender, DataGridItemEventArgs e) { myclass mine = (myclass) e.Item.DataItem; int cellindex = 5; e.Item.Cells[cellindex].Text = mine.mydict["thekey"]; }
2:
I am not familiar with the works of the DataGrid bindings but binding in general can be done on any public property. Try binding to Key
and Value
on the Dictionary
3:
Another option is to add a property to your class that returns the value for that specific key.
public string TheKey { get { return MyDictionary["thekey"]; } }
(The purists out there might not like the idea of adding a property to your object for this reason, but it makes for a simple solution.)