Sort DBGrid Contents With Recordset Refresh

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
See also  11 Effective Keyword Research Tools and Techniques

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist