I'm pretty new to C# and WPF, and my exposure is mostly self-inflicted, so I figure I'm probably missing something obvious. However it appears to be so obvious that it's not showing up in a couple of hours of searching.
Since I'm just trying to work out the processing, all the data is arbitrary, simplistic and a bit silly.
I have this data in a DataTable named dt1:
Name Count Date
Fred 1 12/01/13
Fred 2 12/02/13
Fred 3 12/03/13
Fred 4 12/04/13
Barney 4 12/01/13
Barney 3 12/02/13
Barney 2 12/03/13
Barney 1 12/04/13
Wilma 1 12/01/13
Wilma 2 12/02/13
Wilma 3 12/03/13
Wilma 4 12/04/13
Betty 4 12/01/13
Betty 3 12/02/13
Betty 2 12/03/13
Betty 1 12/04/13
(the columns are string, int and string, respectively)
What I'm trying to do is create a second DataTable with one row for each name, and the count for each date displayed in a separate column, thusly:
Name 12/01/13 12/02/13 12/03/13 12/04/13
Fred 1 2 3 4
...
Here's the code I'm using to populate the second DataTable:
DataTable dt2 = new DataTable();
//add columns for the target dates
dt2.Columns.Add("Name", typeof(string));
for (int n = 1; n < 5; n++)
{
dt2.Columns.Add(String.Format("12/0{0}/13", n.ToString()), typeof(int));
}
DataRow pivotRow = dt2.NewRow();
foreach (DataRow row in dt1.Rows) //step through the rows in the source table
{
if (pivotRow[0].ToString() != row[0].ToString()) //if this is a "new" name in the data set
{
if (pivotRow[0].ToString() != "") //and it's not the first row of the data set
dt2.Rows.Add(pivotRow); //add the row we've been working on
pivotRow = dt2.NewRow(); //create a new row for the "next" name
pivotRow[0] = row[0].ToString(); //add the "next" name to the name column
}
//match the string date stored in column 2 of the source DataTable to the column name in the target one, and put the associated int value in that column
pivotRow[row[2].ToString()] = (int)row[1];
}
//once we've finished the whole source DataTable, add the final row to the target DataTable
dt2.Rows.Add(pivotRow);
//at this point, looking at it through the locals window everything *appears* to be peachy in dt2
GridView.ItemsSource = dt2.DefaultView; //and here, it's all the pits.
This is what displays in the DataGrid:
Name 12/01/13 12/02/13 12/03/13 12/04/13
Fred
Barney
Wilma
Betty
Plainly something is getting saved, since its saving the names at the beginning of the rows, but equally plain is that it's not saving the rest of the data points.
My skull is all mushy from banging against this particular wall, so I've decided to ask for help from folks who know (much) better than I do.
Any insights or suggestions will be greatly appreciated.