[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Clicking Datagrid header to sort column

Posted on 2006-03-21
10
Medium Priority
?
751 Views
Last Modified: 2008-01-09
Hi all,

I have a DataGridView which displays the contents of a table and I can click on the column headers to sort by each of the columns, but what is the code for doing this?

What I mean is, I would like to sort by the second column by clicking a button instead of the column heading. How can I do it?

Freerider.
0
Comment
Question by:Freerider
9 Comments
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 180 total points
ID: 16253054
0
 
LVL 6

Assisted Solution

by:cubixSoftware
cubixSoftware earned 160 total points
ID: 16256365
Hi

If you are not using NET2.0 then you cannot access the DataGrid.Sort and you will have to use DataViews to sort the underlying data that you are binding to.

Here is a link that shows how to implement sorting and searching within the datagrid

http://support.microsoft.com/kb/308070/en-us

HTH :)
0
 

Author Comment

by:Freerider
ID: 16259378
Thanks,

I am using .NET 2.0 - VB2005 Express Edition. I tried using the code from MS link, but I get a few errors.

I managed to get rid of most of them, but still have problems with any line containing
sortorder.???

eg.

If oldColumn Is newColumn AndAlso dgPayments.SortOrder = sortorder.Ascending Then
                direction = System.ComponentModel.ListSortDirection.Descending
            Else

                ' Sort a new column and remove the old SortGlyph.
                direction = System.ComponentModel.ListSortDirection.Ascending
                oldColumn.HeaderCell.SortGlyphDirection = sortorder.None
            End If

returns error " 'Ascending' is not a member of 'String' " & " 'None' is not a member of 'String' "

Any ideas?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 6

Expert Comment

by:JRossi1
ID: 16259399
Why would you want to sort a column by clicking a button instead of the column header?  Just curious.
0
 

Author Comment

by:Freerider
ID: 16259720
JRossi1,

I noticed when I added new records to the dataset they were sorting in the wrong order (the last entry wasn't last in the grid). Your question prompted me to check the SQL statement and noticed it was sorting by Date and not Invoice_ID (which is what I need)

The sort order can be changed using the column headers, but I need to make sure the datagrid is being sorted by Invoice_ID before I can identify the last entry, so I thought the best way of doing it was to use code to sort the datagrid. There might be a better way?...
0
 
LVL 6

Expert Comment

by:JRossi1
ID: 16259834
Did you add 'ORDER BY Invoice_ID' to your SQL Statement?
0
 

Author Comment

by:Freerider
ID: 16259917
Yes. Previously it was 'ORDER BY Date_Completed'
0
 
LVL 6

Assisted Solution

by:JRossi1
JRossi1 earned 160 total points
ID: 16260157
Try this:

        datagridview.Columns(0).HeaderCell.SortGlyphDirection = SortOrder.Ascending

where 0 = the index of the column you want sorted.

This is an example given for SortGlyphDirection in the VS2005 Help:

Public Property SortGlyphDirection As SortOrder
Dim value As SortOrder
value = instance.SortGlyphDirection

instance.SortGlyphDirection = value


0
 

Author Comment

by:Freerider
ID: 16490199
I gave up trying the suggestions as I kept getting errors and didn't think it was important enough to persevere.

Actually, if the datagrid is sorted by Invoice_ID, then any new records will be added to the end of the datagrid.
So, my original question is really irrelevant (and I don't feel was answered)
However, I think it would be fair to split the points with those who offered help.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Screencast - Getting to Know the Pipeline
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question