c# - 数据表不保留附加值

标签 c# wpf datagrid datatable datarow

我对 C# 和 WPF 还很陌生,而且我的接触主要是我自己造成的,所以我想我可能错过了一些明显的东西。然而,它似乎如此明显,以至于在几个小时的搜索后都没有出现。

由于我只是想进行处理,因此所有数据都是任意的、简单化的并且有点愚蠢。

我在名为 dt1 的 DataTable 中有此数据:

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

(列分别为 string、int 和 string)

我想要做的是创建第二个 DataTable,其中每个名称占一行,每个日期的计数显示在单独的列中,因此:

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

这是我用来填充第二个数据表的代码:

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

很明显,有些东西正在被保存,因为它在行的开头保存了名称,但同样明显的是,它没有保存其余的数据点。

我的头骨因撞到这堵特定的墙壁而变得糊状,所以我决定向比我了解(多)多的人寻求帮助。

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

最佳答案

如果您在调试时查看输出窗口,您应该会在网格尝试显示数据时注意到大量绑定(bind)错误。问题在于“/”字符用于解析与底层对象的绑定(bind),因此它无法从您的 View 中获取数据。

您可以通过替换 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);

关于c# - 数据表不保留附加值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20982251/

相关文章:

WPF 文本 block ,文本属性中的换行符

ASP.NET DataGrid 和自定义分页

winforms - Infragistics 列大小调整有问题

c# - 如何使用泛型修复这个非常简单的 .NET 代码以返回强类型结果?

c# - 我的 MVC3 razor View 中的 if 语句存在问题

c# - 值之间的平滑过渡(缓入/缓出)

c# - .NET 在执行调用需要较低权限的 COM+ 的特定代码块时降级管理员权限或模拟当前用户

WPF Datagrid 双击 RowDetailsTemplate 也会触发父行上的双击事件

c# - WPF 工具提示放置目标返回 null

silverlight - 具有 CellTemplate 和绑定(bind)的自定义 DataGrid 列