Reputation: 477
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.
Upvotes: 1
Views: 794
Reputation: 31383
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.)
Upvotes: 0
Reputation: 25083
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"];
}
Upvotes: 1
Reputation: 1470
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
Upvotes: 0