Here is my code for binding a dictionary item to a Chart control. I keep getting the following error:
"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"
Here is my Code:
foreach (DataRow drow in objD0s.Tables[0].Rows)// Adding values from a dataset to dictionary
{
string strvalue = Convert.ToString(drow["Chemical Name"]);
string doublevalue = Convert.ToString(drow["Global Warming"]);
Cdata.Add(Convert.ToString(strvalue), Convert.ToDouble(doublevalue));
}
ColumnSeries colSeries = ChartChemImpact.Series[0] as ColumnSeries;
((ColumnSeries)ChartChemImpact.Series[0]).DataContext = Cdata;
colSeries.ItemsSource = myDataTable0.DefaultView;
colSeries.IndependentValueBinding = new Binding("[Chemical Name]");
colSeries.DependentValueBinding = new Binding("[Global Warming]");
}
XAM
charting开发者_StackOverflow社区Toolkit:Chart Name="ChartChemImpact" Title="Chart Title" Width="384" Height="280">
<chartingToolkit:ColumnSeries DependentValuePath="Key" IndependentValuePath="Value" ItemsSource="{Binding}" Name="colSeries" />
</chartingToolkit:Chart>
Please help :(
Ok, I have found the mistake. There are binding and value paths in the xaml, but they are replaced in the code.
I would remove those lines, so here is the result:
foreach (DataRow drow in objD0s.Tables[0].Rows)// Adding values from a dataset to dictionary
{
string strvalue = Convert.ToString(drow["Chemical Name"]);
string doublevalue = Convert.ToString(drow["Global Warming"]);
Cdata.Add(Convert.ToString(strvalue), Convert.ToDouble(doublevalue));
}
((ColumnSeries)ChartChemImpact.Series[0]).DataContext = Cdata;
//And that's the end of the function, no more code
}
精彩评论