Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

datagrid sorting... 1

Status
Not open for further replies.

QTip

Programmer
Mar 7, 2001
66
BE
Hello,

I have a datagrid with data from my database. Now I want when users click on the headline of a row, that the datagrid sorts the data by the headline that is clicked.
Example: There is a row called "id", when they click on "id" ... all the data should be sorted by id. Is this possible? Thnx!!
 
Yes definitely possible.

Use the HeadClick event to trap the action - then use the ColIndex property to determine which column then the .datafield property to establish which field then Sort method on underlying recordset.
 
Strictly that should be ColIndex parameter - i.e. as passed to the HeadClick event.
 
You will need a client side cursor for this, otherwise, you can change the recordset.source property and re-open the recordset.

Also, you might want to save the last sort order and column index, so you can toggle between ascending and descending if the previous head click was on the same column.
 
Try this one:


Option Explicit
' Declarations
' ============

Private byt_ColSort As Byte
' Holds the number of the column that was last sorted ascending.
' byt_ColSort can have a value from zero to 255,
' so the sub can handle up to 255 columns.
' In case more are needed, change the type to Integer
' =============================================================================
' Subs and/or functions
' =====================

Private Sub grd_GridName_HeadClick(ByVal ColIndex As Integer)
' grd_GridName has of course to be replaced
' with the actual name of the grid

Dim str_SortOrder As String
' If the columnindex equals the value of byt_ColSort, then
' the chosen column was last sorted ascending, so now we'll
' sort it descending
' If a column header was clicked other then the previous one,
' the newly chosen column will be sorted ascending

If byt_ColSort = ColIndex + 1 Then
' Sorted ascending ==> Sort descending
str_SortOrder = " desc"
byt_ColSort = 0
Else
' Sorted descending ==> Sort ascending
str_SortOrder = " asc"
byt_ColSort = ColIndex + 1
End If
' Now let's sort the recordset connected to the datagrid
' according to the header just clicked
rs_GridRecords.Sort = rs_GridRecords.Columns(ColIndex).DataField & str_SortOrder
End Sub
[/code]

Merlijn is the name, and logic is my game...
 
Somehow the code above won't run or even compile for me.

Private Sub DataGrid1_headclick(colindex As Integer)

Code:
End Sub

gives me the following upon attempting to run the application:

"Compile error: Procedure does not match description of event or procedure having the same name."

Checked the MSDN help (and the example) on this and can't figure out what I'm doing wrong. As far as I can tell I'm not using an ambiguous name for my datagrid, the event is correct and so is the number of arguments included. Any ideas?


"Much that I bound, I could not free. Much that I freed returned to me."
(Lee Wilson Dodd)
 
Mmm, don't know what caused the forum to put an 'ÿ' in my post but that place was meant to say "insert code here". :)


"Much that I bound, I could not free. Much that I freed returned to me."
(Lee Wilson Dodd)
 
oops [lol]

You must end the code section with

/code (enclosed in brackets)
 
Got the code working now, apparently the difference between running and not running was in including ByVal. Would still like to know why that made a difference (not positive yet on the practical use of ByVal), but I'm glad to report it's working just fine for me.

The customer's going to be very satisfied as until today, I thought the standard datagrid wasn't capable of this and I have been telling them that whenever they asked for a sorting option!


"Much that I bound, I could not free. Much that I freed returned to me."
(Lee Wilson Dodd)
 
Ah, I messed it up by putting brackets then :) I didn't actually intend to use any kind of formatting but I did put "insert code here" with brackets instead of quotation marks.

"Much that I bound, I could not free. Much that I freed returned to me."
(Lee Wilson Dodd)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top