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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort a datagrid 1

Status
Not open for further replies.

Denaeghel

Programmer
Apr 23, 2001
61
BE
How can I sort a datagrid when I click on the colomnheader.
 
How did you populate the grid. Through code or did you bind it to an ado control?
 
This is the way that I accompolished this.

The datagird has a .HeadClick method. Build your code there. I don't have the exact code with me, but I think this is right or pretty darn close.

** First place your original Select statement for the ado control in a string variable, let's say strSelect.

dgTest.HeadClick(ByVal ColIndex As Integer)
strSort as String
strSort = " order by " &
dgTest.Columns(ColIndex).DataField
adoControlTest.RecordSource = strSelect & strSort
adoControlTest.Refresh

This will retrieve the datafield that is related to the column that you had selected. Then build your order by clause(or you can just put concatenate it to the end of the select statement). Then by making it the new recordsource select and refresh your adoControl you will get the sort recordset by the column that you selected.

Please let me know if this helps and if you have any questions or concerns.
 
I find this to be an easy solution for your datagrid sort problem. I think it is very self-explanatory:

Private Sub dgrje_HeadClick(ByVal ColIndex As Integer)
Dim rs As ADODB.Recordset
Set rs = adorje.Recordset
If rs.Sort <> dgrje.Columns(ColIndex).DataField & &quot; ASC&quot; Then
rs.Sort = dgrje.Columns(ColIndex).DataField & &quot; ASC&quot;
Else
rs.Sort = dgrje.Columns(ColIndex).DataField & &quot; DESC&quot;
End If
End Sub
 
just curious, how do you grab the row that you clicked, so that you can grab the value of a colum on that row (for example, I am using datagrid as a generic view with partial data, I want to double click, and grab one of the field's number, so I can pass it to another dataform to show detailed information) Karl Blessing aka kb244{fastHACK}
kblogo.jpg
 
There are probably several ways of doing it, but I do mine this way in a bound datagrid:

dim strData as string

strData = adoData.Recordset.Fields(IndexNO)

Think of the row as an array with index numbers. Index number (0) being the first column on your grid.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top