Solved

Binding a DataTable to a WPF DataGrid

Posted on 2012-03-27
1
1,675 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
ID: 37774995
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Simple Linear Regression
Introduction to Processes

632 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