Solved

Binding a DataTable to a WPF DataGrid

Posted on 2012-03-27
1
1,592 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

Suggested Solutions

Title # Comments Views Activity
bound data table problem 2 33
VB.NET String Settings and Temp Folder Question 3 54
Convert Ctime to date time in textfile? 7 31
Get month and date in a format 4 31
This article will show, step by step, how to integrate R code into a R Sweave document
A short article about a problem I had getting the GPS LocationListener working.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

860 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