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

前端 未结 15 1004
误落风尘
误落风尘 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:46

    Just in case somebody still looks for it, I did it on VS 2008 C#.

    On the Event ColumnHeaderMouseClick, add a databinding for the gridview, and send the order by field like a parameter. You can get the clicked field as follows:

    dgView.Columns[e.ColumnIndex].Name
    

    In my case the header's names are similar to view field names.

提交回复
热议问题