I'm trying to sort my table by columns. None of these command seems to work. Is it possible to sort by two columns.
DataGridView2.Sort(1, "Ascending")
DataGridView2.Sort(DataGridView2.SelectedColumns(1), "Ascending")
Printable View
I'm trying to sort my table by columns. None of these command seems to work. Is it possible to sort by two columns.
DataGridView2.Sort(1, "Ascending")
DataGridView2.Sort(DataGridView2.SelectedColumns(1), "Ascending")
dday9 - your solution sorts the table, not the DataGridView.
formlesstree4 - your response brings me back to my code which I'm having problems.
I believe this is the correct code to sort the DataGridView. The parameters are not correct.
DataGridView2.Sort(1, "Ascending")
DataGridView2.Sort(DataGridView2.SelectedColumns(1), "Ascending")
DataGridView2.Sort(1, "Ascending")
-- syntax error
DataGridView2.Sort(DataGridView2.SelectedColumns(1), "Ascending")
-- Message="Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"
(5 column table)
If you had read the documentation for the Sort method, you would realize why you're getting the error you are:
There are TWO ways to Sort on a DGV:
AndCode:Sort(IComparer)
Nowhere does the Sort routine have an overload that accepts an Integer and String variable type! What you should be doing is close to what your second item is.Code:Sort(dataGridViewColumn As DataGridViewColumn, direction As ListSortDirection)
Code:DataGridView2.Sort(DataGridView2.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
formlesstree4, thank you for your clarification. I read the info but was unable to understand it. Do you know if it is possible to sort two columns?
From glancing over the documentation, the SortedColumn Property seems to imply only one column can be sorted. However, I could be wrong.
I really can't get my head around the documentation.
I even copied and pasted the example from the MSDN website into my code and changed the names of the identifiers to my code and it still didn't work.
and I get this error DataGridView control cannot be sorted if it is bound to an IBindingList that does not support sorting.
if I use this. Dim CurrentColumn As DataGridViewColumn = InvoiceDataGrid.CurrentCell.OwningColumn
Dim direction As ListSortDirection
direction = ListSortDirection.Ascending
InvoiceDataGrid.Sort(CurrentColumn, direction)
Hello,
Check out Generic sortable binding list in VB.