Skip to main content
 首页 » 编程设计

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

2024年02月13日32del

我有一个“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; 

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

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

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

请您参考如下方法:

不幸的是,此行为在 DataGridView 中不可用控制。为了使用List<T>作为绑定(bind)源并允许列点击排序,那么你需要处理 ColumnHeaderMouseClick 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; 
    } 
} 

注意:默认排序标准是Name .