Binding to DataTable

WPF

Windows Presentation Foundation controls

Binding to DataTable

  • rated by 0 users
  • This post has 19 Replies |
  • 4 Followers
  • 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...

  • No never got it working, we have already gone with another component.
  • 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!

    John Franco

    johnf@componentone.com

    Customer Engagement Manager

    www.componentone.com

  • Thanks very much. This did the trick. 

  • Thanks Alex, this is all I needed. Had it working in a few minutes! 

  • WPFChartDataBoundDoc.zip

     Hi all,

    I am attaching an updated tutorial. Just some minor changes to the intro paragraphs.

    Thanks!

    John Franco

    johnf@componentone.com

    Customer Engagement Manager

    www.componentone.com

  • Private _ChartData1 As New ChartData

    Private Sub Window1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated

    Me.C1Chart1.Data = _ChartData1

    End Sub

    Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded

    ' create connection and fill data set

    Dim mdbFile As String = "c:\Program Files\ComponentOne Studio.NET 2.0\Common\nwind.mdb"

    Dim connString As String = String.Format("Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}", mdbFile)

    Dim conn As New OleDbConnection(connString)

    Dim adapter As New OleDbDataAdapter("SELECT TOP 10 ProductName, UnitPrice" & Chr(13) & "" & Chr(10) & " FROM Products ORDER BY UnitPrice DESC;", conn)

    Dim DataSet As New DataSet

    Dim Series1 As New DataSeries

    adapter.Fill(DataSet, "Products")

    ' set source for chart data

    With _ChartData1

    .ItemsSource = DataSet.Tables("Products").Rows

    .ItemNameBinding = New Binding("[ProductName]")

    .Children.Add(Series1)

    Series1.ValueBinding =
    New Binding("[UnitPrice]")

    End With

    End Sub

  • 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:

     Binding Issue

    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="Window1" Height="300" Width="300" xmlns:c1chart="http://schemas.componentone.com/xaml/c1chart" Loaded="Window_Loaded">

    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>

  • I figured it out. I am running XP 64 and there is no jet 4.0 for 64 bit. I just had to compile it as a x86 app and it ran like a champ.
  • Hi all,

    I have posted this question at a few places on this forum, but this seems to be the perfect place to ask it.

    I had  few questions regarding binding the chart to a data table in the database on the server. I can connect to the database and display its data in a datagrid, however, I cannot get the chart to work with the database. Can you please help me with it?  I have several columns in the table which ahould be the different series on my chart.

                 XYDataSeries ds = new XYDataSeries();

                 ds.Label = "Series 1";

                 ds.ItemsSource = DataDT.DefaultView;

                 ds.ValueBinding = new Binding("Dayofweek");

                 ds.XValueBinding = new Binding("DO_MOD_MAPE");

                 c1Chart1.Data.Children.Add(ds);

    Here is a small piece of code. Let me know if I am on the wrong track completely. I am new to using WPF..

    Awaiting ur reply.

    Thanks,

    Rucha!

Page 1 of 2 (20 items) 12