I am a beginner.
I am developing a program in C# with Visual Studio and Forms. My Form has a DataGridView linked to a BindingList. I would like to be able to sort rows by clicking a column header.
I set the SortMode to Automatic, but it doesn’t work : there’s no glyph in the column header and clicking on it won’t sort the rows.
Here is my code :
public class SimpleTask
{
public string Title { get; set; }
public string CategoryName { get; set; }
}
//Code in my Form :
BindingSource tasksBinding = new BindingSource();
//uiTaskGridView is the name of my DataGridView
uiTaskGridView.AutoGenerateColumns = false;
//add one column
var taskNameColumn = new DataGridViewTextBoxColumn
{
DataPropertyName = "Title",
HeaderText = "Titre",
Width = 300,
Resizable = DataGridViewTriState.False,
SortMode = DataGridViewColumnSortMode.Automatic
};
uiTaskGridView.Columns.Add(taskNameColumn);
//create bindinglist and populate
var bindingList = new BindingList<SimpleTask>();
bindingList.Add(new SimpleTask { Title = "Complete project", CategoryName = "Work" });
bindingList.Add(new SimpleTask { Title = "Buy groceries", CategoryName = "Personal" });
tasksBinding.DataSource = bindingList;
uiTaskGridView.DataSource = tasksBinding;
Result is :
enter image description here
Anybody can tell me why I can’t sort the “Title” column?
Thanks a lot.
Chandernagor is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.