Home Forums WPF controls Xceed DataGrid for WPF DataBinding to DataTable with C# Code

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #21978 |

    Hi

    I’m searching for an working example of Data binding to a ADO.NET Data Table using C# code. I’m not sure if I have to use the DataContext property or whatever.

    Thanks for your help.

    Thomas

    Imported from legacy forums. Posted by Thomas (had 3888 views)

    Xceed Support
    Member
    Post count: 5658

    Hi,

    Here is a basic sample on how to bind to ADO.NET DataTable:
    <code>
    DataSet dataSet = new DataSet();
    string mdbFile = @”Data\Northwind.mdb”;
    string connString = String.Format( “Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}”, mdbFile );

    OleDbConnection conn = new OleDbConnection( connString );

    OleDbDataAdapter adapter = new OleDbDataAdapter();
    adapter.SelectCommand =
    new OleDbCommand(
    “SELECT * FROM Employees;”, conn );

    adapter.Fill( dataSet, “Employees” );

    myDataGridControl.ItemsSource = dataSet.Tables[ “Employees” ];
    </code>

    Imported from legacy forums. Posted by Marcus [Xceed] (had 283 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi Marcus

    Thanks for your sample. I thought in the same direction, but it doesn’t work.

    The assignment to the ItemSource failed because of type mismatch.

    –> myDataGridControl.ItemsSource = dataSet.Tables[ “Employees” ];

    ItemSource has to be of type System.Collections.IEnumerable.

    What’s wrong?

    Thomas

    Imported from legacy forums. Posted by Thomas (had 291 views)

    Xceed Support
    Member
    Post count: 5658

    Hmmm,

    I guess I was a little quick on the reply button!!! 😉

    You can either do this:

    <code>

    myDataGridControl.ItemsSource = dataSet.Tables[ “Employees” ].DefaultView;

    </code>

    Or through a Binding ( the conversion will extract the DefaultView).

    <code>

    <DataGridControl ItemsSource=”{Binding}”/>
    <!– Valid if the DataTable was set as the DataContext, as you stated in your initial post –>

    </code>

    OR

    <code>

    <DataGridControl ItemsSource=”{Binding ElementName=myWindow, Path=MyDataTable}”/>
    </code>

    Imported from legacy forums. Posted by Marcus [Xceed] (had 290 views)

    User (Old forums)
    Member
    Post count: 23064

    Thanks! Now it works.

    Thomas

    Imported from legacy forums. Posted by Thomas (had 5365 views)

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.