Documents

NextGrid & NextDBGrid

NextGrid

NextDBGrid

NextInspector

NextSheet

NextCollection

Misc

Labs

NextGrid .NET

Sorting records in NextDBGrid

Related Articles:

Info
  • Skill:
  • Version: 1.0.0
  • Author: Bojan Nikolic
  • Created: 2009-02-26
  • Updated: 2009-02-26
Overview

NextDBGrid does't have integrated sorting routine like NextGrid. To sort data, NextDBGrid get help from DataSet. Adding sorting capabilities to DBGrid is very easy, but may be different for some protocols when working with Tables (not Queries).

Implementing Sorting Routine



For adding sort capability to NextDBGrid, following code need to be added to OnSortColumn event:

Sorting Query



procedure TForm1.NextDBGrid1SortColumn(Sender: TObject; ACol: Integer;
  Ascending: Boolean);
var 
  DirectionStr: string;
  QueryString: string;
begin
  QueryString := 'SELECT * FROM EMPLOYEE'; // Query statement
  if NextDBGrid1.Columns[ACol].FieldName = '' then Exit;
  if Ascending then DirectionStr := 'ASC' else DirectionStr := 'DESC';  
  Query1.SQL.Text := QueryString   ' ORDER BY '   NextDBGrid1.Columns[ACol].FieldName
    ' '   DirectionStr;
  Query1.Open;
end;


Sorting ADO Table



procedure TForm1.NextDBGrid1SortColumn(Sender: TObject; ACol: Integer;
  Ascending: Boolean);
var
  DirectionStr: string;
begin
  if NextDBGrid1.Columns[ACol].FieldName = '' then Exit;
  if Ascending then DirectionStr := 'ASC' else DirectionStr := 'DESC';  
  ADOTable1.Sort := NextDBGrid1.Columns[ACol].FieldName   ' '   DirectionStr;  ADOTable1.Active := True;
end;

Was This Article Useful?

Comments

2010-09-15 07:11:37

can i sort by two columns same time?

for example, 3 columns date,item name, value1,valuen....

sorting every column after date, it sorts first by selected column and then same time by date descending?

2010-09-15 12:10:20

Hello,

Unfortunately this is not possible at the moment.

Best regards
Only constructive comments, code contributions... will be publishes. Questions, non-official discussion will not be published.