c# - 如何对绑定(bind)到 WinForm 列表的 DataGridView 中的列进行排序?

标签 c# winforms

我的表单中有一个 DataGridView,并且我将该 DataGridView 绑定(bind)到一个列表。 该列设置为 SortMode=Automatic

这是我的代码

List<string> lstLines = new List<string>();
StreamReader sr = new StreamReader(tbFile.Text);
while ((line = sr.ReadLine()) != null)
{
  lstLines.Add(line);
}
dgvLines.DataSource = lstLines.Select(x=>new {Text=x}).ToList();

当我单击列标题时,我希望对列进行排序,但什么也没发生。

最佳答案

你可以试试这个......

对数据源为通用List的datagridview列进行排序的完整代码

//-------------------------------------------------------- -------------------------------------------------------- //在表单中 - 在构造函数或表单加载中,填充网格。 //------------------------------------------------ --------------------------------------------------------

    List<student> students;

    private void PopulateList()
    {
        student std1 = new student("sss", 15, "Female");
        student std2 = new student("ddd", 12, "Male");
        student std3 = new student("zzz", 16, "Male");
        student std4 = new student("qqq", 14, "Female");
        student std5 = new student("aaa", 11, "Male");
        student std6 = new student("lll", 13, "Female");

        students = new List<student>();
        students.Add(std1);
        students.Add(std2);
        students.Add(std3);
        students.Add(std4);
        students.Add(std5);
        students.Add(std6);

        dataGridView1.DataSource = students;
    }

//-------------------------------------------------------- ------------------------------------------------

//Comparer类根据列名和排序顺序进行排序 //------------------------------------------------ ----------------------------------------------------------

class StudentComparer : IComparer<Student>
{
    string memberName = string.Empty; // specifies the member name to be sorted
    SortOrder sortOrder = SortOrder.None; // Specifies the SortOrder.

    /// <summary>
    /// constructor to set the sort column and sort order.
    /// </summary>
    /// <param name="strMemberName"></param>
    /// <param name="sortingOrder"></param>
    public StudentComparer(string strMemberName, SortOrder sortingOrder)
    {
        memberName = strMemberName;
        sortOrder = sortingOrder;
    }

    /// <summary>
    /// Compares two Students based on member name and sort order
    /// and return the result.
    /// </summary>
    /// <param name="Student1"></param>
    /// <param name="Student2"></param>
    /// <returns></returns>
    public int Compare(Student Student1, Student 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 "Sex":
                if (sortOrder == SortOrder.Ascending)
                {
                    returnValue = Student1.Sex.CompareTo(Student2.Sex);
                }
                else
                {
                    returnValue = Student2.Sex.CompareTo(Student1.Sex);
                }
                break;
            default:
                if (sortOrder == SortOrder.Ascending)
                {
                    returnValue = Student1.Name.CompareTo(Student2.Name);
                }
                else
                {
                    returnValue = Student2.Name.CompareTo(Student1.StudentId);
                }
                break;
        }
        return returnValue;
    }
}

//-------------------------------------------------------- ------------------------------------------------

//单击列标题进行排序 //------------------------------------------------ ----------------------------------------------------------

    private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        //get the current column details
        string strColumnName = dataGridView1.Columns[e.ColumnIndex].Name;
        SortOrder strSortOrder = getSortOrder(e.ColumnIndex);

        students.Sort(new StudentComparer(strColumnName, strSortOrder));
        dataGridView1.DataSource = null;
        dataGridView1.DataSource = students;
        customizeDataGridView();
        dataGridView1.Columns[e.ColumnIndex].HeaderCell.SortGlyphDirection = strSortOrder;
    }

   /// <summary>
    /// Get the current sort order of the column and return it
    /// set the new SortOrder to the columns.
    /// </summary>
    /// <param name="columnIndex"></param>
    /// <returns>SortOrder of the current column</returns>
    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;
        }
    }

希望对你有帮助......

关于c# - 如何对绑定(bind)到 WinForm 列表的 DataGridView 中的列进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8011481/

相关文章:

c# - 重命名正在运行的应用程序 - 危险吗?

c# - 我如何填写我的半圈?

c# - PictureBox 控件中的翻转图像(镜像)

winforms - 在 WinForms MDI 中使用具有存储库模式的 Entity Framework

c# - 使用 Windows 打印图像对话框打印图像

c# - 在 C# 中打印 Form/UserControl

c# - GetFiles 没有得到正确的文件

c# - 从线程中的 GUI 对象检索属性

c# - MySql 数据库插入日期时

.net - 带复选框的下拉列表