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...
public partial class psysOPS001 : System.Windows.Controls.Page
{
delegate void UIUpdaterDelegate();
private void displayResults()
{
System.Data.DataTable dTable = new System.Data.DataTable("OPS001");
this.pgeDataGrid.Dispatcher.Invoke(new UIUpdaterDelegate(() =>
{
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)));
}));
_intMaxNoOfRows++;
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); }));
}
}
this.pgeDataGrid.Dispatcher.Invoke(new UIUpdaterDelegate(() =>
{
this.pgeDataGrid.ItemsSource = dTable.DefaultView ;
}
));