【问题标题】:Displayed ListView as PivotData (XAML, C#)将 ListView 显示为 PivotData(XAML、C#)
【发布时间】:2019-04-01 07:28:08
【问题描述】:

我已经在ListView里面显示了数据:

<ListView ItemsSource="{Binding Path=ListUsers}" >
    <ListView.View>
        <GridView>
         <GridViewColumn Header="ReadTime" DisplayMemberBinding="{Binding ReadTime}" />
         <GridViewColumn Header="Stanowisko" DisplayMemberBinding="{Binding Position}" />
         <GridViewColumn Header="Counter" DisplayMemberBinding="{Binding PosCou}" />
        </GridView>
    </ListView.View>
</ListView>

看起来像:

ReadTime | Position |PosCou
---------------------------
01022019 |  PosA    | 10
01022019 |  PosB    | 20
01022019 |  PosC    | 30
...
01032019 |  PosA    | 12
01032019 |  PosB    | 21
01032019 |  PosC    | 33

现在我想displayed exactly the same data 但在视图中像Pivot Data

      |01022019 | 01032019 | ...
---------------------------------
PosA  |  10     |   12     | ...
PosB  |  20     |   21     | ...
PosC  |  30     |   33     | ...
...

我该怎么做?

我的 Linq 查询:

 public IList<GroupUser> GetList(ICollection<User> UserList)
        {
            GroupUsersList = UserList
                            .GroupBy(x => new { x.ReadTime, x.Position })
                            .Select(group => new GroupUser
                                            {
                                                ReadTime = group.Key.ReadTime,
                                                Position = group.Key.Stanowisko,
                                                PosCou = group.Count()
                                            })
                            .ToList();
            return new List<GroupUser>(GroupUsersList);
        }

IEnumerable&lt;User&gt; listUsers:

public class User
{
 public string ReadTime { get; set; }
 public string Id { get; set; }
 public string LName { get; set; }
 public string FName { get; set; }
 public string Position { get; set; }
 public string City { get; set; }
 public string Country { get; set; }
 public string Car { get; set; }
}

【问题讨论】:

  • 你在哪里挣扎?你试过什么?
  • 你在使用 MVVM 吗?您可以直接在数据库中透视数据吗?
  • 是的 MVVM,我有 Linq 查询(请参阅更新)

标签: c# wpf xaml listview pivot


【解决方案1】:

我该怎么做?

您使用表示透视视图中的一行的类型并转换您的源集合。如果列数是动态的,您可以使用DataTable

给定一个IEnumerable&lt;User&gt;,下面的方法应该返回一个旋转的DataTable

private static DataTable TransformData(IEnumerable<User> listUsers)
{
    DataTable dataTable = new DataTable();
    dataTable.Columns.Add(new DataColumn("Position"));

    Dictionary<string, DataRow> positions = new Dictionary<string, DataRow>();
    foreach (var user in listUsers)
    {
        DataColumn column = dataTable.Columns.OfType<DataColumn>().FirstOrDefault(c => c.ColumnName == user.ReadTime);
        if (column == null)
        {
            column = new DataColumn(user.ReadTime);
            dataTable.Columns.Add(column);
        }

        DataRow row;
        if (!positions.TryGetValue(user.Position, out row))
        {
            row = dataTable.NewRow();
            dataTable.Rows.Add(row);
            row[0] = user.Position;
            positions.Add(user.Position, row);
        }

        object o = row[column];
        int posCou = o == DBNull.Value ? 0 : Convert.ToInt32(o);
        row[column] = posCou + user.PosCou;
    }

    return dataTable;
}

然后您可以将ListView 替换为自动生成动态列的DataGrid,或者在视图中自己显式创建列:

<DataGrid ItemsSource="{Binding Path=DataTable.DefaultView}" IsReadOnly="True" />

【讨论】:

  • 在我的 MVVM 到connect ListView 我做了ICollection&lt;GroupUser&gt; ListUsers { get; set;}....现在如何处理DataTable.DefaultView
  • 只需从属性返回DataViewIEnumerablepublic DataView ListUsers =&gt; dataTable.DefaultView;
  • 我收到错误:An object reference is required for the non-static field...
  • 知道了,现在是int posCou = o == DBNull... => Value was either too large or too small for an Int32.
  • 那么在调试代码时o 的值是多少?另请注意,我提供了一个示例。 SO 不是代码编写服务...
猜你喜欢
  • 2019-10-25
  • 2018-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-23
  • 2021-04-13
  • 1970-01-01
相关资源
最近更新 更多