Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.

I am in the process of converting a Winform project to WPF-XBAP. I've come up against a brick-wall in trying to bind a datatable to a data grid on one of my pages.
To set the scene, I have a 2-D array that holds information calculated using code-behind C#. The array has 24 rows and 10 columns.
I create and load a datatable object with the contents of the 2-D array.
When running the project in debug mode, I see that the datatable is loaded correctly with ALL rows and column holding data as expected. All good so far!

I finally bind the datatable to the page datagrid. This is where it goes screwy. The datagrid shows columns 1, 2 & 10 , but columns 3,4,5,6,7,8,& 9 are ALL blanks!!!

I've tried ALL SORTS of things to get all columns to appear on the datagrid, but to no avail!!
If some clever chap can see what I'm doing wrong, I would be eternally grateful.
Here's the bit of C# code where I attempt to bind the datagrid ('pgeDataGrid')to the datatable ('dTable'). You will notice I'm using a Background worker, hence the use of the delegate class...

C#
public partial class psysOPS001 : System.Windows.Controls.Page
{
       delegate void UIUpdaterDelegate();



        /* This method attempts to display the final results to the datagrid*/
       private void displayResults()
       {
           /* Create a new DataTable;*/
           System.Data.DataTable dTable = new System.Data.DataTable("OPS001");


           this.pgeDataGrid.Dispatcher.Invoke(new UIUpdaterDelegate(() =>
           {
               /*add columns to the dataTable (10 in total)*/
               dTable.Columns.Add(new  DataColumn("Nation",  typeof(String)));
               dTable.Columns.Add(new  DataColumn("Females", typeof(String)));
               dTable.Columns.Add(new  DataColumn("Males",   typeof(String)));
               dTable.Columns.Add(new  DataColumn("Elig. 1", typeof(String)));
               dTable.Columns.Add(new  DataColumn("Elig. 2", typeof(String)));
               dTable.Columns.Add(new  DataColumn("Elig. 3", typeof(String)));
               dTable.Columns.Add(new  DataColumn("Elig. 4", typeof(String)));
               dTable.Columns.Add(new  DataColumn("Elig. 5", typeof(String)));
               dTable.Columns.Add(new  DataColumn("Elig. 6", typeof(String)));
               dTable.Columns.Add(new  DataColumn("Total"  , typeof(String)));

           }));

           /* now add the rows */
           _intMaxNoOfRows++; // increment this to get the Totals row..

           for (int i = 0; i < _intMaxNoOfRows; i++)
           {
               System.Data.DataRow dataRow = dTable.NewRow();

               for (int j = 0; j < _maxJ; j++)
               {
                   if (!string.IsNullOrWhiteSpace(strArrayList[i, 0]))
                       dataRow[j] = strArrayList[i, j];

               }

               if (!string.IsNullOrWhiteSpace(strArrayList[i, 0]))
               {
                this.pgeDataGrid.Dispatcher.Invoke(new UIUpdaterDelegate(() =>
                   { dTable.Rows.Add(dataRow); }));
               }

           }

           /* Try to bind the datatable to the datagrid */
           this.pgeDataGrid.Dispatcher.Invoke(new UIUpdaterDelegate(() =>
           {

                this.pgeDataGrid.ItemsSource = dTable.DefaultView ;
              /* Not all data is being bound! */


           }
           ));
Posted
Updated 26-Jan-15 0:35am
v2
Comments
George Swan 26-Jan-15 13:24pm    
Try setting the DataGrid's DataContext in code
this.pgeDataGrid.DataContext = dTable.DefaultView ;
And setting the ItemsSource in the Xaml
<datagrid name="pgeDataGrid" itemssource="{Binding}"></datagrid>
Eric_2709 27-Jan-15 0:56am    
Hi George.

I tried your suggestions, but alas, no joy! I have, however, found a solution to this problem! I write the contents of the 2D-array to a SQL table, then using a SqlDataAdapter, I fill the datagrid! Works a treat! :-)
All rows and columns are properly displayed on the datagrid!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900