如何在C#中显示和排序列表?

时间:2013-11-05 02:48:47

标签: c# datagridview

我有一份“StudentRecords”列表,其中包含学生及其考试成绩。对于每个学生,我都有他们的姓名,年龄,联系电话,考试成绩以及参加考试的日期。

public class StudentRecord
{
       public String Name  { get; set; }
       public int Age  { get; set; }
       public String PhoneNum  { get; set; }
       public int TestScore1  { get; set; }
       public DateTime TestScore1Date  { get; set; }
}

List<StudentRecord> StudentList = new List<StudentRecord>();
dataGridView1.DataSource = StudentList;

我将List绑定到DataGridView控件,并且能够很好地查看信息。现在,我希望能够通过名称首先排序列表的内容,然后分数,然后在DataGrid控件中对年龄进行排序。

我发现了这个: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.columnheadermouseclick.aspx

表示默认行为是根据点击的列标题排序网格行。但是,单击时默认情况下不会发生这种情况。事实上,当我点击列标题时没有任何反应。知道我可能做错了吗?

3 个答案:

答案 0 :(得分:3)

不幸的是,DataGridView控件无法立即使用此行为。为了使用List<T>作为绑定源并允许列单击排序,您需要处理ColumnHeaderMouseClick的{​​{1}}事件,如下所示:

DataGridView

注意:默认排序条件为protected void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) { // Get the information about the column clicked var strColumnName = dataGridView1.Columns[e.ColumnIndex].Name; SortOrder strSortOrder = getSortOrder(e.ColumnIndex); // Sort the list StudentList.Sort(new StudentComparer(strColumnName, strSortOrder)); // Rebind to use sorted list dataGridView1.DataSource = null; dataGridView1.DataSource = StudentList; // Update user interface icon for sort order in column clicked dataGridView1.Columns[e.ColumnIndex].HeaderCell.SortGlyphDirection = strSortOrder; } private SortOrder getSortOrder(int columnIndex) { if (dataGridView1.Columns[columnIndex].HeaderCell.SortGlyphDirection == SortOrder.None || dataGridView1.Columns[columnIndex].HeaderCell.SortGlyphDirection == SortOrder.Descending) { dataGridView1.Columns[columnIndex].HeaderCell.SortGlyphDirection = SortOrder.Ascending; return SortOrder.Ascending; } else { dataGridView1.Columns[columnIndex].HeaderCell.SortGlyphDirection = SortOrder.Descending; return SortOrder.Descending; } } public class StudentComparer : IComparer<StudentRecord> { string memberName = String.Empty; SortOrder sortOrder = SortOrder.None; public StudentComparer(string strMemberName, SortOrder sortingOrder) { memberName = strMemberName; sortOrder = sortingOrder; } public int Compare(StudentRecord student1, StudentRecord student2) { int returnValue = 1; switch (memberName) { case "Name" : if (sortOrder == SortOrder.Ascending) { returnValue = student1.Name.CompareTo(student2.Name); } else { returnValue = student2.Name.CompareTo(student1.Name); } break; case "Age": if (sortOrder == SortOrder.Ascending) { returnValue = student1.Age.CompareTo(student2.Age); } else { returnValue = student2.Age.CompareTo(student1.Age); } break; case "PhoneNum": if (sortOrder == SortOrder.Ascending) { returnValue = student1.PhoneNum.CompareTo(student2.PhoneNum); } else { returnValue = student2.PhoneNum.CompareTo(student1.PhoneNum); } break; case "TestScore1": if (sortOrder == SortOrder.Ascending) { returnValue = student1.TestScore1.CompareTo(student2.TestScore1); } else { returnValue = student2.TestScore1.CompareTo(student1.TestScore1); } break; case "TestScore1Date": if (sortOrder == SortOrder.Ascending) { returnValue = student1.TestScore1Date.CompareTo(student2.TestScore1Date; } else { returnValue = student2.TestScore1Date.CompareTo(student1.TestScore1Date); } break; default: if (sortOrder == SortOrder.Ascending) { returnValue = Student1.Name.CompareTo(Student2.Name); } else { returnValue = Student2.Name.CompareTo(Student1.Name); } break; } return returnValue; } }

答案 1 :(得分:1)

您必须实现自定义代码才能对datagrid进行数据排序。该文档关于默认行为的方法是,当您在设计模式下双击网格时,VS会生成一个方法来处理此事件。 请阅读:Sort DataGrid

答案 2 :(得分:0)

首先,我认为你需要实施这种行为,我怀疑它是开箱即用的。

其次,您可以使用LINQ轻松地对对象进行排序,并拥有不错的群组等。

现在,如果您想根据点击次数按照不同的标准对它们进行排序,您可能需要使用ListView,然后您就可以对其进行过滤,但这通常需要更长的时间。这是MSDN page,在底部你有一个链接,可以点击标题点击...