Bind DataGrid to Dictionary in ASP.NET 



حمایت از ما | مشهدهاست

Bind DataGrid to Dictionary in ASP.NET


حمایت از ما | مشهدهاست

حمایت از ما | مشهدهاست حمایت از ما | مشهدهاست

حمایت از ما | مشهدهاست
`

Bind DataGrid to Dictionary in ASP.NET



asked

6 years ago

viewed

753 times

active

3 years ago


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.


2010-05-06 19:59:04Z


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.)




 اين مطلب بدون دخالت انساني عينا از اين آدرس کپي شده و تمامي مسوليت آن با ناشر اصلي است.

The breakpoint will not currently be hit. No symbols have been loaded for this document in a Silverlight application
WCF - How to Increase Message Size Quota
How to get current page URL in MVC 3
Why is access to the path denied?
Login failed for user 'IIS APPPOOL\ASP.NET v4.0'
How to call javascript function from code-behind

How to add doublequotes to a string that is inside a variable?
Select method in List<t> Collection
Setting dropdownlist selecteditem programmatically
How to write a simple Html.DropDownListFor()?
What is the right way to populate a DropDownList from a database?
A potentially dangerous Request.Path value was detected from the client (*)
*