Hi ,
Could you please give me a quick sample of how to bind to a datatable in code-behind? Can't seem
to find any help on that.
Did you ever make any progress on this? I haven't found any examples either.
Seems as if we are the only people in the world trying to actually use this stuff. I'm about ready to look elsewhere...
Thanks. So far I've been extreamly disappointed in the WPF components. They look beautiful, and I wish I could use them. But, the total lack of help and real world samples, or forums, or support, is just making it not worth it. I submitted a "Support Incident" asking for help on this today, we'll see..... they are losing a customer a little bit (well actually a lot of bit) everyday I waste on this.
Would you mind saying who's component you went with?
Here is the sample code(C# + XAML) that builds chart using data table from NorthWind database.
C# (Creating dataset)... DataSet _dataSet; private void Window_Loaded(object sender, RoutedEventArgs e) { // create connection and fill data set string mdbFile = @"c:\db\nwind.mdb"; string connString = string.Format( "Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}", mdbFile); OleDbConnection conn = new OleDbConnection(connString); OleDbDataAdapter adapter = new OleDbDataAdapter( @"SELECT TOP 10 ProductName, UnitPrice FROM Products ORDER BY UnitPrice;", conn); _dataSet = new DataSet(); adapter.Fill(_dataSet, "Products"); // set data table rows as the source for chart data c1Chart1.Data.ItemsSource = _dataSet.Tables["Products"].Rows; } ...
XAML ( data binding settings)
... <my:C1Chart.Data> <my:ChartData ItemNameBinding="{Binding Path=[ProductName]}"> <my:DataSeries ValueBinding="{Binding Path=[UnitPrice]}"/> </my:ChartData> </my:C1Chart.Data> ...
Hi all,
I am attaching a quick tutorial that our Documentation team put together for Data Binding in WPFChart. (See post further down for updated tutorial)
Thanks!
Thanks very much. This did the trick.
Thanks Alex, this is all I needed. Had it working in a few minutes!
I am attaching an updated tutorial. Just some minor changes to the intro paragraphs.
adapter.Fill(DataSet,
.ItemsSource = DataSet.Tables(
.Children.Add(Series1)
Hi there,
I am trying this example on my system but for some reason the information isnt being rendered (Please see image). There are no build errors or crashes of any kind:
My XAML Code:
-------------------------
<Window x:Class="ChartProgrammatically.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib" Title="Window1" Height="300" Width="500" xmlns:my="clr-namespace:C1.WPF.C1Chart;assembly=C1.WPF.C1Chart" Loaded="Window_Loaded"> <Grid> <my:C1Chart Margin="0" Name="c1Chart1" ChartType="Bar" > <TextBlock DockPanel.Dock="Top" Text="Ten Most Expensive Products" HorizontalAlignment="Center"/> <my:C1Chart.Data> <my:ChartData ItemNameBinding="{Binding Path=[ProductName]}"> <my:DataSeries ValueBinding="{Binding Path=[UnitPrice]}" /> </my:ChartData> </my:C1Chart.Data> </my:C1Chart></Grid></Window>
-----------------------------------------------------
My C#
-------------
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;using System.Data;using System.Data.OleDb;using C1.WPF.C1Chart;namespace ChartProgrammatically{ /// <summary> /// Interaction logic for Window1.xaml /// </summary> public partial class Window1 : Window { public Window1() { InitializeComponent(); } public DataSet _dataSet; private void Window_Loaded(object sender, RoutedEventArgs e) { // create connection and fill data set string mdbFile = @"D:\03 Projects\ChartProgrammatically\nwind.mdb"; string connString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}", mdbFile); OleDbConnection conn = new OleDbConnection(connString); OleDbDataAdapter adapter = new OleDbDataAdapter(@"SELECT TOP 10 ProductName, UnitPrice FROM Products ORDER BY UnitPrice DESC;", conn); _dataSet = new DataSet(); adapter.Fill(_dataSet, "Products"); // set source for chart data c1Chart1.Data.ItemsSource = _dataSet.Tables["Products"].Rows; } }}
-------------------------------
If anyone could give any suggestions that would be great.
Regards.
Lee.
Hi Lee,
I just asked the developer and he told me the following:
The Loaded event is not set, so the code in Window_Loaded() never runs.
Please add event attribute in Xaml:
<Window Loaded="Window_Loaded" ...
Or in the code of constructor:
...
public Window1()
{
InitializeComponent();
Loaded +=new RoutedEventHandler(Window_Loaded);
}
So you can add the event attribute in XAML so it appears like the following:
Title
Please let me know if this helps. I apologize if I forgot to include the Window_Loaded event attribute in the steps.
--Kristy
I'm trying to walk through this and am getting a blank chart as well. I checked to see that my Nwind.mdb path is correct and that it is running the window_loaded event. Code is below.
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;using System.Data;using System.Data.OleDb;using C1.WPF.C1Chart;
namespace Chart_Test{ /// <summary> /// Interaction logic for Window1.xaml /// </summary> public partial class Window1 : Window { public Window1() { InitializeComponent(); } DataSet _dataSet;
private void Window_Loaded(object sender, RoutedEventArgs e) { // create connection and fill data set string mdbFile = @"C:\Program Files (x86)\ComponentOne Studio.NET 2.0\Common\Nwind.mdb"; string connString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}", mdbFile); OleDbConnection conn = new OleDbConnection(connString); OleDbDataAdapter adapter = new OleDbDataAdapter(@"SELECT TOP 10 ProductName, UnitPrice FROM Products ORDER BY UnitPrice DESC;", conn); _dataSet = new DataSet(); adapter.Fill(_dataSet, "Products");
// set source for chart data c1Chart1.Data.ItemsSource = _dataSet.Tables["Products"].Rows; }
private void button1_Click(object sender, RoutedEventArgs e) { string test = "teesting"; }
}}
<Window x:Class="Chart_Test.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib" Title="Window1" Height="448" Width="774" Loaded="Window_Loaded" xmlns:my="clr-namespace:C1.WPF.C1Chart;assembly=C1.WPF.C1Chart"> <Grid> <my:C1Chart Content="" Margin="0,0,86,0" Name="c1Chart1" ChartType="Bar"> <my:C1Chart.Data> <my:ChartData ItemNameBinding="{Binding Path=[ProductName]}"> <my:DataSeries ValueBinding="{Binding Path=[UnitPrice]}"/> </my:ChartData> </my:C1Chart.Data> <my:Legend DockPanel.Dock="Right" /> </my:C1Chart> <TextBlock DockPanel.Dock="Top" Text="Ten Most Expensive Products" HorizontalAlignment="Center"/><Button Height="23" HorizontalAlignment="Right" Margin="0,139,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click">Button</Button> </Grid></Window>