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

Set all the column's (which can be sortable by users) SortMode property to Automatic.

Set all the column's (which can be sortable by users) SortMode property to Automatic dataGridView1. DataSource = students. Select(s => new { ID = s.

StudentId, RUDE = s. RUDE, Nombre = s. Name, Apellidos = s.

LastNameFather + " " + s. LastNameMother, Nacido = s. DateOfBirth }) .

OrderBy(s => s. Apellidos) .ToList(); foreach(DataGridViewColumn column in dataGridView1. Columns) { dataGridView1.Columnscolumn.Name.

SortMode = DataGridViewColumnSortMode. Automatic; } Edit: As your datagridview is bound with a linq query, it will not be sorted. So please go through this link which explains how to create a sortable binding list and to then feed it as datasource to datagridview.

– delete Apr 5 at 14:20 @Sergio: I have edited the answer – Marshal Apr 5 at 14:24 @Niraj: I get a compiler error. I tried changing your answer to column.Name but it doesn't seem to modify the sorting in any way. – delete Apr 5 at 14:26 @Sergio: Can you say what error do you receive?

– Marshal Apr 5 at 14:28 @Niraj: You edited your answer. Before it was just 'column' and it would fire a compiler error. I changed it to column.

Name to make it work then you changed your answer. However this still doesn't allow me to sort the information. – delete Apr 5 at 14:30.

You can use DataGridViewColoumnHeaderMouseClick event like this : Private string order = String. Empty; private void dgvDepartment_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) { if (order == "d") { order = "a"; dataGridView1. DataSource = students.

Select(s => new { ID = s. StudentId, RUDE = s. RUDE, Nombre = s.Name, Apellidos = s.

LastNameFather + " " + s. LastNameMother, Nacido = s. DateOfBirth }) .

OrderBy(s => s. Apellidos).ToList(); } else { order = "d"; dataGridView1. DataSource = students.

Select(s => new { ID = s. StudentId, RUDE = s. RUDE, Nombre = s.

Name, Apellidos = s. LastNameFather + " " + s. LastNameMother, Nacido = s.

DateOfBirth }. OrderByDescending(s => s. Apellidos) .ToList() } }.

As Niraj suggested, use a SortableBindingList. I've used this very successfully with the DataGridView. Here's a link to the updated code I used - Presenting the SortableBindingList - Take Two Just add the two source files to your project, and you'll be in business.

Source is in SortableBindingList.zip.

1. Create a class which contains all properties you need, and populate them in the constructor class Student { int _StudentId; public int StudentId {get;} string _Name; public string Name {get;} ... public Student(int studentId, string name ...) { _StudentId = studentId; _Name = name; ... } } 2. Create an IComparer class, to be able to sort class StudentSorter : IComparer { public enum SField {StudentId, Name ... } SField _sField; SortOrder _sortOrder; public StudentSorder(SField field, SortOrder order) { _sField = field; _sortOrder = order;} public int Compare(Student x, Student y) { if (_SortOrder == SortOrder.

Descending) { Student tmp = x; x = y; y = tmp; } if (x == null || y == null) return 0; int result = 0; switch (_sField) { case SField. StudentId: result = x.StudentId. CompareTo(y.

StudentId); break; case SField. Name: result = x.Name. CompareTo(y.Name); break; ... } return result; } } 3.

Within the form containing the datagrid add ListDictionary sortOrderLD = new ListDictionary(); //if less than 10 columns private SortOrder SetOrderDirection(string column) { if (sortOrderLD. Contains(column)) { sortOrderLDcolumn = (SortOrder)sortOrderLDcolumn == SortOrder. Ascending?

SortOrder. Descending : SortOrder. Ascending; } else { sortOrderLD.

Add(column, SortOrder. Ascending); } return (SortOrder)sortOrderLDcolumn; } 4. Within datagridview_ColumnHeaderMouseClick event handler do something like this private void dgv_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) { StudentSorter sorter = null; string column = dGV.Columnse.ColumnIndex.

DataPropertyName; //Use column name if you set it if (column == "StudentId") { sorter = new StudentSorter(StudentSorter.SField. StudentId, SetOrderDirection(column)); } else if (column == "Name") { sorter = new StudentSorter(StudentSorter.SField.Name, SetOrderDirection(column)); } ... List lstFD = datagridview. DataSource as List; lstFD.

Sort(sorter); datagridview. DataSource = lstFD; datagridview.Refresh(); } Hope this helps.

I had problems using the "enter code here" , if someone could properly format it that would be great, thanks. – Albert Apr 5 at 16:42 Just select the text and click the button with the two curly braces. – Greg Sansom Apr 6 at 6:13.

Void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) { foreach (DataGridViewColumn clm in dataGridView1. Columns) { clm. SortMode = DataGridViewColumnSortMode.

Automatic; } } simple as that..Since I am new not so sure though...

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions