Binding a DataSet to a MultiColumn MS Chart
I have a DataSet with multiple DataTables that I'd like to bind to a chart with multiple column series. I expected each series to have different values respective to the DataTables in the DataSets; however, when the chart appears, both series display the same values in the Y column, the values from the first tables.
Here is a general idea what my code is like:
(I created and filled up a DataSet named diffCharts, diffCharts contains tables table1 and table2, both tables have columns Month开发者_Go百科 and Amount)
Chart1.Series.Add("table1");
Chart1.Series["table1"].XValueMember = "Month";
Chart1.Series["table1"].YValueMembers = "Amount";
Chart1.Series.Add("table2");
Chart1.Series["table2"].XValueMember = "Month";
Chart1.Series["table2"].YValueMembers = "Amount";
Chart1.DataSource = diffCharts;
Chart1.DataBind();
Am I missing something? I've never bound a DataSet with multiple tables to a chart before.... Does MS know how to handle this?
I know this is an old post, but better late than never.
You can bind enumerable data sources directly to the DataPointCollection property in the series using the DataBind method. This way each series can be bound to a different table. I am relatively new to C#, so there may be an adverse affect to this method that I am not aware of. It functions correctly in my limited testing.
MSDN link.
Chart1.Series.Add("table1");
Chart1.Series["table1"].Points.DataBind(table.AsEnumerable(), "nameofXaxis", "nameofYaxis, "nameofPropertyaxis");
Propertyaxis can be left as an empty string
I don't think you can do that.
You'll probably need to create a table with one column as the shared column for the X Axis (Month) and then two more columns for the Y values you want to graph.
The DataTable class has a Merge function that may do exactly what you need.
精彩评论