I am trying to get the sorting to work for the DataGridView. The sorting should be automatic when I click on the column headers but it is not working. What am I doing wrong?
private void LoadDummyData()
{
List<AddressBookDummy> list = new List<AddressBookDummy>();
list.Add(new AddressBookDummy { Name = "Newman, Alfred", Type = "CAR" });
list.Add(new AddressBookDummy { Name = "Skywalker, Luke", Type = "SUP" });
list.Add(new AddressBookDummy { Name = "Skywalker, Leia", Type = "BEN" });
addressBookGrid.DataSource = list;
}
private void InitializeGrid()
{
addressBookGrid.RowHeadersVisible = false;
addressBookGrid.ScrollBars = ScrollBars.Vertical;
addressBookGrid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
addressBookGrid.Columns[0].SortMode = DataGridViewColumnSortMode.Automatic;
addressBookGrid.Columns[1].SortMode = DataGridViewColumnSortMode.Automatic;
}
You have to bind to a list that implements sorting, here’s an example
Summary :