Solved

Binding a DataTable to a WPF DataGrid

Posted on 2012-03-27
1
1,532 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
Comment Utility
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This is about my first experience with programming Arduino.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now