Solved

Binding a DataTable to a WPF DataGrid

Posted on 2012-03-27
1
1,566 Views
Last Modified: 2012-08-14
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
0
Comment
Question by:pcpal4
1 Comment
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
ID: 37774995
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question