简体   繁体   English

排序datagrid列。 对象列表C#

[英]Sorting datagrid column. List of objects c#

I have a list of students that is assigned to a datagrid. 我有分配给数据网格的学生列表。 Each student has ID, last name and first name. 每个学生都有ID,姓氏和名字。 I need to allow the user to select from id or last name sorting order. 我需要允许用户从ID或姓氏排序顺序中进行选择。 I know that it's most probably gonna be through .Sort but i'm not sure how to connect it to surname or ID. 我知道这很可能会通过.Sort,但是我不确定如何将其连接到姓氏或ID。

students = new List<Student>();

students = new List<Student>();
students.Add(new Student() { GroupID = "Alpha", StFName = "Name", StLName = "Surname"});
students.Add(new Student() { GroupID = "Beta", StFName = "Foo", StLName = "Surname"});
students.Add(new Student() { GroupID = "Beta", StFName = "Bar", StLName = "Surname"});
students.Add(new Student() { GroupID = "Gamma", StFName = "Baz", StLName = "Surname"});

Thanks (: 谢谢 (:

students.OrderBy(x=>x.GroupID);

to order descending 订购降序

students.OrderByDescending(x=>x.GroupID);

to combine 结合

students.OrderBy(x=>x.GroupID).ThenBy(x=>x.StLName);

It's not pretty straight-forward. 这不是很简单。 Maybe you add a "Windows Form" tag to this question in order to get more responses from people with more experience in Windows Forms. 也许您在此问题上添加了“ Windows窗体”标签,以便获得更多具有Windows窗体经验的人的更多答复。 However, you can still give your users the option to select the sorting field through a UI element such as a DropDownList (or ComboBox...I dont even know what the control name is in Windows Forms). 但是,您仍然可以为用户提供通过UI元素(例如DropDownList(或ComboBox ...我什至不知道Windows窗体中的控件名称))选择排序字段的选项。 Then, based on the user selection you can do a switch in c# to order your list (data source) by the field specified in the sorting options. 然后,基于用户选择,您可以在c#中进行切换,以通过排序选项中指定的字段对列表(数据源)进行排序。

Let me know if it's cleared enough and if you need a code snippet 让我知道它是否已清除足够,以及您是否需要代码段

Leo 狮子座

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

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