Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Visual Basic
Expertise: Intermediate
Apr 27, 2000

Sort DBGrid Contents With Recordset Refresh

It's often useful to sort a DBGrid field in either descending or ascending order. You do this by using the HeadClick event and the DataField property of columns. You must change the query string (Qry) with one of your own (be sure it contains the code in bold):
 
Private Sub DBGrid1_HeadClick(ByVal ColIndex As Integer)
	Dim Qry as string
	Qry = "SELECT * FROM MyTable WHERE Key='" _
		& txtKey & "' "
	Qry = Qry & "ORDER BY " & _
		DBGrid1.Columns(ColIndex).DataField _
		& " " & DBGrid1.Tag
	data1.RecordSource = Qry
	data1.Refresh
	DBGrid1.ReBind
	'toggle ASC and DESC keywords
	If DBGrid1.Tag = "ASC" Then DBGrid1.Tag _
		= "DESC" Else DBGrid1.Tag = "ASC"
End Sub
Juan Jose
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date