Reputation: 91
Long story short I have to populate a grid with dynamic data (including dynamic columns). Which is possible when binding the radgrid (telerik tool) to a list of anonymous types. But I need to add properties to the anonymous object from items read from the database.
Dim grdds2 = (From grd As PeerReviewReportByCategoryItems In GetPRReportByCategory(dpkStartDate.SelectedDate, dpkEndDate.SelectedDate, "")
Select New With {
.CategoryName = grd.CategoryName,
.Total = grd.TotalCount,
.CategoryID = grd.CategoryID,
.Test = grd.RadiologistCountsString}
)
RadGrid1.DataSource = grdds2
For example here I create an anonymous type and bind it to the grid, works perfectly.
But now I need to add properties to the anonymous type with names/values that are read from the database.
Is this possible?
Upvotes: 0
Views: 341
Reputation: 1499840
Rather than creating an anonymous type, create an ExpandoObject
(assuming you're using .NET 4). That's designed specifically for "I want to set a bunch of properties dynamically".
When you know the property name up-front, just use it like any other dynamic type. When you want to access them via variable values (e.g. read from the database) just cast it to IDictionary(Of Object, Of Object)
and you can set/get values that way.
EDIT: As noted in comments, whether this is bound properly (and automatically) to the grid will depend on whether RadGrid supports dynamic typing. There are ways for it to find out the dynamically-supplied properties, but whether it does or not is a different matter.
Upvotes: 1