Skip to content

Commit 90d881b

Browse files
github-actions[bot]KB Bot
andauthored
Added new kb article htmlchart-bind-dynamic-expando-list (#676)
Co-authored-by: KB Bot <[email protected]>
1 parent 0d6cb78 commit 90d881b

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
---
2+
title: Using Dynamic ExpandoObject as DataSource in RadHtmlChart
3+
description: Learn how to bind a dynamic ExpandoObject list to the DataSource of a RadHtmlChart in ASP.NET AJAX applications.
4+
type: how-to
5+
page_title: Binding Dynamic Expando List to RadHtmlChart DataSource in ASP.NET AJAX
6+
slug: htmlchart-bind-dynamic-expando-list
7+
tags: radhtmlchart, asp.net ajax, datasource, databind, expandoobject, datatable
8+
res_type: kb
9+
ticketid: 1684733
10+
---
11+
12+
## Environment
13+
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td>Product</td>
18+
<td>RadHtmlChart for ASP.NET AJAX</td>
19+
</tr>
20+
<tr>
21+
<td>Version</td>
22+
<td>All</td>
23+
</tr>
24+
</tbody>
25+
</table>
26+
27+
## Description
28+
29+
I have created a dynamic "expando" object list and assigned it to the DataSource of a RadHtmlChart. No error occurs, but the HTMLChart remains empty. In CodeLens, I see the DataSource is available on the RadHTMLChart. Is this type of DataSource supported?
30+
31+
32+
This knowledge base article also answers the following questions:
33+
34+
- How to bind a RadHtmlChart to a dynamic data source in ASP.NET AJAX?
35+
- Can I use an ExpandoObject as a DataSource for RadHtmlChart?
36+
- How to convert an ExpandoObject list to a DataTable for RadHtmlChart binding?
37+
38+
## Solution
39+
40+
The `ExpandoObject` is not directly supported as a data source for the RadHtmlChart due to the dynamic creation of its properties. However, you can convert an `ExpandoObject` list to a `DataTable`, which is a supported data source for the RadHtmlChart. Follow these steps to convert and bind the data:
41+
42+
1. Convert the `ExpandoObject` list to a `DataTable`.
43+
44+
2. Bind the `DataTable` to the RadHtmlChart.
45+
46+
Here's the implementation:
47+
48+
````C#
49+
protected void Page_Load(object sender, EventArgs e)
50+
{
51+
if (!IsPostBack)
52+
{
53+
RadHtmlChart1.DataSource = GetDataSource();
54+
RadHtmlChart1.DataBind();
55+
}
56+
}
57+
58+
private DataTable GetDataSource()
59+
{
60+
List<ExpandoObject> dataSource = GetExpandoObjectData();
61+
DataTable dataTable = ExpandoListToDataTable(dataSource);
62+
63+
return dataTable;
64+
}
65+
66+
private DataTable ExpandoListToDataTable(List<ExpandoObject> list)
67+
{
68+
DataTable dataTable = new DataTable();
69+
70+
if (list == null || list.Count == 0) return dataTable;
71+
72+
// Use the first item to define the columns
73+
IDictionary<string, object> firstRow = list[0];
74+
foreach (string key in firstRow.Keys)
75+
{
76+
dataTable.Columns.Add(key, firstRow[key].GetType() ?? typeof(object));
77+
}
78+
79+
// Populate the rows
80+
foreach (ExpandoObject item in list)
81+
{
82+
IDictionary<string, object> dict = item;
83+
DataRow row = dataTable.NewRow();
84+
foreach (string key in dict.Keys)
85+
{
86+
row[key] = dict[key] ?? DBNull.Value;
87+
}
88+
dataTable.Rows.Add(row);
89+
}
90+
91+
return dataTable;
92+
}
93+
94+
private List<ExpandoObject> GetExpandoObjectData()
95+
{
96+
// Example data creation using ExpandoObject
97+
dynamic row1 = new ExpandoObject();
98+
row1.X_Axis_Categories = "Label 1";
99+
row1.First_Series_Values = 1;
100+
row1.Second_Series_Values = 5;
101+
102+
dynamic row2 = new ExpandoObject();
103+
row2.X_Axis_Categories = "Label 2";
104+
row2.First_Series_Values = 10;
105+
row2.Second_Series_Values = 15;
106+
107+
return new List<ExpandoObject> { row1, row2 };
108+
}
109+
````
110+
111+
3. Ensure your RadHtmlChart markup is set up to use the columns from your DataTable as data fields:
112+
113+
````ASP.NET
114+
<telerik:RadHtmlChart runat="server" ID="RadHtmlChart1" Height="300px">
115+
<PlotArea>
116+
<Series>
117+
<telerik:LineSeries DataFieldY="First_Series_Values" Name="first series"></telerik:LineSeries>
118+
<telerik:LineSeries DataFieldY="Second_Series_Values" Name="second series"></telerik:LineSeries>
119+
</Series>
120+
<XAxis DataLabelsField="X_Axis_Categories"></XAxis>
121+
</PlotArea>
122+
</telerik:RadHtmlChart>
123+
````
124+
125+
## See Also
126+
127+
- [Data Binding Overview for RadHtmlChart](https://www.telerik.com/products/aspnet-ajax/documentation/controls/htmlchart/data-binding/overview#data-binding-overview)
128+
- [DataTable Class in .NET](https://docs.microsoft.com/en-us/dotnet/api/system.data.datatable?view=netframework-4.8)

0 commit comments

Comments
 (0)