|
-
Apr 12th, 2013, 11:29 AM
#1
Thread Starter
Addicted Member
DataGridView sorting
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")
-
Apr 12th, 2013, 12:31 PM
#2
Re: DataGridView sorting
<waiting on google...>
C# using a binding source - here
vb.net using a datatable - here
vb.net question with a c# answere - here
-
Apr 12th, 2013, 12:38 PM
#3
Re: DataGridView sorting
When in doubt, consult the official MSDN website (or search engine of your flavor) for sorting a DataGridView. Surprisingly (or not), I found that via this simple search as the first result.
-
Apr 12th, 2013, 03:36 PM
#4
Thread Starter
Addicted Member
Re: DataGridView sorting
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")
-
Apr 12th, 2013, 03:44 PM
#5
Re: DataGridView sorting
 Originally Posted by holisticsam
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")
How are they not correct? Do you get an error message or does nothing happen?
-
Apr 12th, 2013, 04:01 PM
#6
Thread Starter
Addicted Member
Re: DataGridView sorting
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)
-
Apr 12th, 2013, 04:12 PM
#7
Re: DataGridView sorting
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:
And
Code:
Sort(dataGridViewColumn As DataGridViewColumn, direction As ListSortDirection)
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:
DataGridView2.Sort(DataGridView2.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
-
Apr 12th, 2013, 04:43 PM
#8
Thread Starter
Addicted Member
Re: DataGridView sorting
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?
-
Apr 12th, 2013, 04:46 PM
#9
Re: DataGridView sorting
 Originally Posted by holisticsam
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.
-
Apr 13th, 2013, 04:32 AM
#10
Member
Re: DataGridView sorting
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.
-
Apr 13th, 2013, 04:48 AM
#11
Member
Re: DataGridView sorting
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)
-
Apr 15th, 2013, 09:13 AM
#12
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|