将WPF饼图绑定到sql server

时间:2013-01-08 12:05:33

标签: c# wpf xaml pie-chart

我使用WPF Toolkit创建了一个饼图。 我必须从我的数据库表中提取Ram Usage的值。 在我的MainWindow.xaml.cs中,我创建了一个数据库连接字符串来建立数据库连接,如下所示:

   public partial class MainWindow : Window
  {
    SqlConnection con;
    SqlDataAdapter da_Ram=null;
    DataSet ds_Ram = null;
    public MainWindow()
    {
        InitializeComponent();
        try
        {
         con = new SqlConnection("Data Source=User212;Initial Catalog=UsageDB;Persist Security Info=True);
         con.Open();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

    private void myChart1_Loaded(object sender, RoutedEventArgs e)
    {
        string selectStatement = "select * from Used,Total from UsageDB.dbo.RamUsage";
        da_Ram = new SqlDataAdapter(selectStatement, con);
        ds_Ram = new DataSet();
        // I am stuck here to proceed for filling my Dataset
    }

} }

XAML代码:

   <loc1:Chart Name="myChart1" Background="YellowGreen" Foreground="DarkBlue" 
     Title="Ram Usage - Pie Chart" LegendTitle="Usage Details" Loaded="myChart1_Loaded" Margin="26,12,12,12">
   <loc1:PieSeries Title="Ram Usage" ItemsSource="{Binding RamUsage}"
               IndependentValueBinding="{Binding Path=Total}"
               DependentValueBinding="{Binding Path=Used}">
    </loc1:PieSeries>
  </loc1:Chart>

这种做法是否正确?或者我是否必须创建一个单独的类来检索数据源中的值?感谢。

0 个答案:

没有答案