devxlogo

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

devx-admin

Share the Post: