简体   繁体   English

如何通过C#中的值将listView中的项目分组?

[英]How to group items in a listView by a value in C#?

I have a listView and I want to group items that share the same ID but different SysDate so that instead of showing multiple lines with the same value and different dates, I want to show multiple lines only when the ID changes. 我有一个listView,我想对共享相同ID但SysDate不同的项目进行分组,因此,我不想显示具有相同值和不同日期的多行,而仅当ID更改时才显示多行。

Example: 例:

From this: 由此:

ID | Sysdate
1  | 12:00
1  | 12:01
1  | 12:02
2  | 12:03
2  | 12:05

To this with only Sysdate changing for the same line: 为此,只有Sysdate更改同一行:

ID | Sysdate
1  | 12:00
2  | 12:05

Like if the current item that's been added is the same as the one that already exists in the listview then only change the sysdate in the same line, if not then add that new line in the listview . 就像如果当前添加的项目与listview中已经存在的当前项目相同,则仅更改同一行中的sysdate ,否则,请在listview添加该新行。

Your question as stated is unanswerable. 如上所述,您的问题无法回答。 There exists a concept of "grouping" in Windows ListView , (it is for example what you see in Windows Explorer if you open up "My Computer" and select "View" -> "Content",) but I do not think that's what you have in mind. Windows ListView存在“分组”的概念(例如,如果您打开“我的电脑”并选择“视图”->“内容”,则是在Windows资源管理器中看到的内容),但我认为这不是事实你要记住。 And besides that concept of "grouping", there is no other defined "grouping" operation that can be performed on a ListView . 除了“分组”的概念外,没有其他可以对ListView执行的已定义“分组”操作。

Your question does not help in defining what you mean by grouping, because for each group of ids (1,2) you have picked a sysdate to display arbitrarily. 您的问题无助于定义分组的含义,因为对于每组id(1,2),您都选择了一个sysdate来任意显示。 (For the case where id=1 you have picked 12:00, which is on the first row with id=1, but in the case where id=2 you have picked 12:05, which is on the second row with id=2.) (对于id = 1的情况,您选择了12:00,位于id = 1的第一行,但是对于id = 2的情况,您选择了12:05,位于id = 2的第二行2.)

I would suggest that you consider grouping a property of your dataset, and not a property of the ListView . 我建议您考虑对数据集的属性进行分组,而不是对ListView的属性进行分组。 In other words, first think exactly what you mean by grouping, then come up with code that performs the grouping on your dataset, and then feed the ListView with the grouped dataset. 换句话说,首先要仔细考虑分组的含义,然后提出对数据集执行分组的代码,然后将分组数据集提供给ListView The ListView cannot possibly know anything about your concept of grouping. ListView可能对您的分组概念一无所知。

var obj = objTest.OrderBy(x => x.ID).GroupBy(x => x.ID).Select(g => new {g, count = g.Count()}).SelectMany(t => t.g.Select(b=>b).Zip(Enumerable.Range(t.count, 1), (j, i) => new {j.Sysdate, j.ID}));

If your dataobject has an ID and a sysdate, and thats how you show them, you can't group them. 如果您的数据对象有一个ID和一个sysdate,这就是您如何显示它们,则无法对其进行分组。 Now if your dataobject has an ID and a List of sysdates, you only get 1 occurance of each ID in your ListView and with each ID you get a List of sysdates. 现在,如果您的数据对象具有一个ID和一个sysdate列表,则在ListView中每个ID仅出现1次,而每个ID则得到一个sysdates列表。 You will have to code your own ViewCell for this. 您将必须为此编写自己的ViewCell。

If you get your dataobjects from a query, you will have to format the dataobjects yourself. 如果从查询中获取数据对象,则必须自行格式化数据对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM