How to enable DataGridView sorting when user clicks on the column header?

前端 未结 15 1008
误落风尘
误落风尘 2020-12-02 15:28

I have a datagridview on my form and I populate it with this:

dataGridView1.DataSource = students.Select(s => new { ID = s.StudentId, RUDE = s.RUDE, Nombr         


        
15条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-02 15:43

    I suggest using a DataTable.DefaultView as a DataSource. Then the line below.

    foreach (DataGridViewColumn column in gridview.Columns)
        {
           column.SortMode = DataGridViewColumnSortMode.Automatic;
        }
    

    After that the gridview itself will manage sorting(Ascending or Descending is supported.)

提交回复
热议问题