Link to home
Start Free TrialLog in
Avatar of pcpal4
pcpal4Flag for United States of America

asked on

Binding a DataTable to a WPF DataGrid

I have a WPF C# (,Net Framework 4) application.   I have added a WPF DataGrid to a xaml window.  In my code I run a query against an oracle database which returns a DataTable object.  I need to programmatically bind this to a WPF DataGrid in order to display the data on the window.  The user will also need to be able to make edits to the WPF DataGrid (i.e add row, update data in cells, etc.) and have the code save these changes back to the database.  How do I do this????  

XAML
 (there are more columns to the datagrid but I didn't want to copy them all
here.  I also made sure that my x:Name and xUid were set to the same name as the columns in the sql query which become the column names in the datatable.

<DataGrid x:Uid="MyDataGrid" x:Name="MyDataGrid" Grid.Column="0" Grid.Row="1" Height="160" Width="1104" Margin="10,5,0,10"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto"
CanUserReorderColumns="False"
IsEnabled="true" IsReadOnly="False"
HorizontalAlignment="Stretch">
       <DataGrid.Columns>
             <DataGridTextColumn x:Name="SYS_ID" x:Uid="SYS_ID" Visibility="Hidden" Header="ID" CanUserResize="False" />
             <DataGridTextColumn x:Name="SYS_ID2" x:Uid="SYS_ID2" Visibility="Hidden" Header="ID2" CanUserResize="True"  />
           <DataGridTextColumn x:Name="ID" x:Uid="ID" Visibility="Visible" Header="ID" CanUserResize="True" />
         <DataGridTextColumn x:Name="CODE" x:Uid="CODE" Visibility="Visible" Header="Code" CanUserResize="True"  />
         <DataGridTextColumn x:Name="COMMENT_TXT" x:Uid="COMMENT_TXT" Visibility="Visible" Header="Comment" CanUserResize="True"   />
        <DataGridTextColumn x:Name="NOTES_TXT" x:Uid="NOTES_TXT" Visibility="Visible" Header="Notes" CanUserResize="True" />
      </DataGrid.Columns>


C# Code

DataTable myDT = new DataTable("MyDT");

...  run oracle query and it fills the myDT datatable ...

if (myDT.Rows.Count > 0)
{
             ??? Need to bind multiple rows in DataTable to the WPF DataGrid so the DataGrid
                  will display the data on the window
}
else
{
            ??? If there are no rows in the database the user will need to add them to the
                 DataGrid and save the info back to the database
                  ? can a datagrid be editable with no rows  
                  ? will I have to add some sort of "Add" button to get the datagrid to accept  
                     editing  of rows and adding rows
}

-------------------------------

I tried to bind the DataTable to the DataGrid using both teh ItemsSource and DataContext but all that seems to happen is one of 2 things:
1. The DataContext rows count = DataTable row count but nothing shows on the window
    AthleteTermDataGrid.DataContext = athTerms.AsEnumerable();

2. I end up with a System.Windows XML Parse Exception
ASKER CERTIFIED SOLUTION
Avatar of BuggyCoder
BuggyCoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial