Mega Code Archive

 
Categories / Delphi / ADO Database
 

Sort DBGrid on column click !

Title: Sort DBGrid on column click ! Question: How to resort a dataset with a mouse-click... Answer: It's quite easy to do this. I have a TQuery, TDatasource and TDbGrid on a form, linked together. QuerySQL is a global string that holds the SQL-statement. begin QuerySQL := 'SELECT * FROM Customer.DB'; Query1.SQL.Add(QuerySQL); Query1.Open; end; In the DBGrid event OnTitleClick, we just add an ORDER-BY clause to the sql and refresh the query. procedure TForm1.DBGrid1TitleClick(Column: TColumn); begin witzh Query1 do begin DisableControls; Close; SQL.Clear; SQL.Add(QuerySQL); SQL.Add('ORDER BY ' + Column.FieldName); Open; // Restore the title settings, otherwise everything // will be blue after a while DBGrid1.Columns.RestoreDefaults; Column.Title.Font.Color := clBlue; EnableControls; end; end;