C1 Community
ComponentOne Community is a free source for developers and help authors to collaborate and communicate.

Binding to DataTable

rated by 0 users
This post has 13 Replies | 3 Followers

Not Ranked
Posts 2
lian182 Posted: Tue, Apr 15 2008 9:18 AM

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.

 

 

Top 500 Contributor
Posts 14
x2miller replied on Thu, May 8 2008 12:09 PM

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

Not Ranked
Posts 2
lian182 replied on Thu, May 8 2008 2:44 PM

No never got it working, we have already gone with another component.
Top 500 Contributor
Posts 14

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? 

Top 10 Contributor
Posts 1,004
C1_AlexT replied on Tue, May 13 2008 5:06 AM

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

Top 25 Contributor
Posts 387
C1_JohnF replied on Tue, May 13 2008 8:56 AM

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
Top 500 Contributor
Posts 14
x2miller replied on Tue, May 13 2008 4:33 PM

Thanks very much. This did the trick. 

Top 500 Contributor
Posts 14
x2miller replied on Tue, May 13 2008 4:36 PM

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

Top 25 Contributor
Posts 387

 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
Not Ranked
Posts 1

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

Not Ranked
Posts 6

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. 

Not Ranked
Posts 12
kllst23 replied on Wed, Mar 25 2009 12:36 PM

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

Top 500 Contributor
Posts 13
pltaylor3 replied on Fri, May 8 2009 10:22 AM

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>

Top 500 Contributor
Posts 13
pltaylor3 replied on Fri, May 8 2009 10:41 AM
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.
Page 1 of 1 (14 items) | RSS
Contact ComponentOne: 1.800.858.2739 ©1987-2010 ComponentOne LLC All Rights Reserved.