WPF 中的 MS Chart,设置数据源不是创建系列

发布于 2024-09-04 10:53:58 字数 1868 浏览 5 评论 0原文

在这里,我尝试分配数据源(使用示例应用程序中给出的相同代码)并创建一个图表,唯一的区别是我在 WPF WindowsFormsHost 中执行此操作。由于某种原因,数据源未正确分配,我无法看到正在创建的系列(“系列 1”)。有线的事情是它可以在 Windows 窗体应用程序中工作,但不能在 WPF 应用程序中工作。

我错过了什么吗?有人可以帮助我吗?

谢谢

<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(myConnectionString);

// 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); 
} 

谢克

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(myConnectionString);

// 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沧笙踏歌 2024-09-11 10:53:58

通过以下两项更改,您可以修复代码:

1 - 而不是

Chart1.Series"Series 1".XValueMember = "Name"; 
Chart1.Series"Series 1".YValueMembers = "Sales";

编写

Chart1.Series["Series 1"].XValueMember = "Name"; 
Chart1.Series["Series 1"].YValueMembers = "Sales";

2 - 在上述代码之前,插入以下行:

Chart1.ChartAreas.Add("Default");
Chart1.Series.Add(new Series("Series 1"));

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"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文