下面是类移调列表<T>使用C#
public class ErrorDTO
{
public string Type { get; set; }
public string Data { get; set; }
}
我ErrorDTO即List<ErrorDTO>
下面的列表是数据
Type1 x
Type1 y
Type2 z
Type3 p
Type2 q
等等....
我想通过以下方式在xaml的DataGrid中显示它:
Type1 Type2 Type3
x z p
y q
我该怎么做?我试图将列表转换为数据表,但没有运气。 invalidDataList是List<ErrorDTO>
var r = invalidDataList
.Distinct()
.GroupBy(x => x.Type)
.Select(y => new { Type = y.Key, Data = y });
DataTable table = new DataTable();
foreach (var item in r)
{
table.Columns.Add(item.Type, typeof(string));
foreach (var i in item.Data)
{
DataRow dr = table.NewRow();
dr[item.Type] = i.Data;
table.Rows.Add(dr);
}
}
dataGrid.ItemsSource = table.AsEnumerable();
[关于与LINQ枢转堆叠的问题](http://stackoverflow.com/questions/18238046/trying-to-pivot-data-using LINQ)尝试搜索与LINQ的支点搜索 – RadioSpace 2014-09-02 23:02:13
这可能有助于你http://stackoverflow.com/questions/320089/how-do-i-bind-a-wpf-datagrid-to-a-variable-number-列 – Seminda 2014-09-03 04:41:12