1

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

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

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

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

4

3 回答 3

3

不幸的是,此行为不适用于控件的开箱即用DataGridView。为了List<T>用作绑定源并允许列单击排序,您需要处理 的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

于 2013-11-05T03:12:44.410 回答
1

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

于 2013-11-05T03:02:53.290 回答
0

首先,我认为您需要实现该行为,我怀疑它是开箱即用的。

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

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

于 2013-11-05T03:03:18.137 回答