我对C#和WPF还是很陌生,而且我的接触大多是自欺欺人的,因此我认为我可能缺少明显的东西。但是,它看起来是如此明显,以至于它在几个小时的搜索中都没有出现。

由于我只是在尝试进行处理,因此所有数据都是任意的,简单的且有点愚蠢的。

我在名为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


(这些列分别是字符串,整数和字符串)

我想做的是创建第二个数据表,每个名称一行,并将每个日期的计数显示在单独的列中,因此:

Name  12/01/13  12/02/13  12/03/13  12/04/13
Fred  1         2         3         4
...


这是我用来填充第二个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.


这是在DataGrid中显示的内容:

Name        12/01/13    12/02/13    12/03/13    12/04/13
Fred
Barney
Wilma
Betty


显然,保存了某些内容,因为它在行的开头保存了名称,但同样清楚的是,它没有保存其余的数据点。

我的头骨撞上了这堵特殊的墙壁,浑身都是糊状的,所以我决定向比我了解得多的人寻求帮助。

任何见解或建议将不胜感激。

最佳答案

如果在调试时查看“输出”窗口,则当网格试图显示数据时,您应该会注意到很多绑定错误。问题在于“ /”字符用于解析与基础对象的绑定,因此无法从您的视图中获取数据。

您可以通过替换ColumnName中的'/'字符,然后将其放在Caption中来使其工作。

//add columns for the target dates
DataTable dt2 = new DataTable();
dt2.Columns.Add("Name", typeof(string));
for (int n = 1; n < 5; n++)
{
    var dataColumn = dt2.Columns.Add(String.Format("12_0{0}_13", n), typeof (int));
    dataColumn.Caption = String.Format("12/0{0}/13", n);
}

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, replacing the '/', and put the associated int value in that column
    pivotRow[row[2].ToString().Replace("/", "_")] = (int)row[1];
}
//once we've finished the whole source DataTable, add the final row to the target DataTable
dt2.Rows.Add(pivotRow);

10-07 12:33
查看更多