Here I am trying to assign the datasource (using same code given in the sample application) and create a graph, only difference is i am doing it in WPF WindowsFormsHost. due to some reason the datasource is not being assigned properly and i am not able to see the series ("Series 1") being created. wired thing is that it is working in the Windows Forms application but not in the WPF one.
am i missing something and can somebody help me?
Thanks
<Window x:Class="SEDC.MDM.WinUI.WindowsFormsHostWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:CHR="clr- namespace:System.Windows.Forms.DataVisualization.Charting;assembly=System.Windows.Forms.Dat aVisualization"
Title="HostingWfInWpf" Height="230" Width="338">
<Grid x:Name="grid1">
</Grid>
</Window>
private void drawChartDataBinding()
{
System.Windows.Forms.Integration.WindowsFormsHost host =
new System.Windows.Forms.Integration.WindowsFormsHost();
string fileNameString = @"C:\Users\Shaik\MSChart\WinSamples\WinSamples\data\chartdata.mdb";
// initialize a connection string
string myConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileNameString;
// define the database query
string mySelectQuery = "SELECT * FROM REPS;";
// create a database connection object using the connection string
OleDbConnection myConnection = new OleDbConnection(myCon开发者_如何学编程nectionString);
// create a database command on the connection using query
OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection);
Chart Chart1 = new Chart();
// set chart data source
Chart1.DataSource = myCommand;
// set series members names for the X and Y values
Chart1.Series"Series 1".XValueMember = "Name";
Chart1.Series"Series 1".YValueMembers = "Sales";
// data bind to the selected data source
Chart1.DataBind();
myCommand.Dispose();
myConnection.Close();
host.Child = Chart1;
this.grid1.Children.Add(host);
}
Shaik
With the following two changes, you can fix your code:
1 - Instead of
Chart1.Series"Series 1".XValueMember = "Name";
Chart1.Series"Series 1".YValueMembers = "Sales";
write
Chart1.Series["Series 1"].XValueMember = "Name";
Chart1.Series["Series 1"].YValueMembers = "Sales";
2 - Before the above code, insert the following lines:
Chart1.ChartAreas.Add("Default");
Chart1.Series.Add(new Series("Series 1"));
精彩评论