简体   繁体   English

单击列标题时,Delphi7 TMS TDBAdvGrid对数据进行排序

[英]Delphi7 TMS TDBAdvGrid Sort data when column header is clicked

I'm a newbie into Delphi and i need an advice. 我是Delphi的新手,我需要建议。

I'm using a TMS TDBAdvGrid and i need to sort the data when the user is clicking the header of a column. 我正在使用TMS TDBAdvGrid,当用户单击列标题时,需要对数据进行排序。 I setup the sort settings of the grid and i write code for the onclicksort event, but it is not working. 我设置了网格的排序设置,并为onclicksort事件编写了代码,但是它不起作用。

The sort settings of the grid: 网格的排序设置:

 SortSettings.Show = True;
 SortSettings.IgnoreBlanks = True;
 SortSettings.BlankPos = blLast;

the onclicksort event: onclicksort事件:

 try
     try
       if FSortISWorking then
         Exit;
       FSortISWorking := true;

       if ACol < 0 then
       begin
         grid.BeginUpdate;
         grid.SortSettings.Column := ACol;
         Application.ProcessMessages;
         grid.QSort;
         grid.EndUpdate;
       end;
     except on e: Exception do
       begin
         // log the error
       end;
     end; 
     finally
      FSortISWorking := false;  
     end;

The grid is not linked directly to the database. 网格未直接链接到数据库。 The data is loaded into memory (TClientDataSet) and i need to sort the data only in memory, without another query to the database. 数据已加载到内存(TClientDataSet)中,我只需要对内存中的数据进行排序,而无需对数据库进行其他查询。

Thank you 谢谢

我尝试了您的示例,这为我解决了问题:

Grid.PageMode := False;

In order to resolve this problem you must order the dataset behind your grid. 为了解决此问题,必须对网格后面的数据集进行排序。 here you have how to do this in general:http://delphi.about.com/od/usedbvcl/l/aa042203a.htm. 在这里,您通常具有以下操作方法:http://delphi.about.com/od/usedbvcl/l/aa042203a.htm。

bellow you have an example: 下面有一个例子:

 procedure TForm1.DBAdvGrid1CanSort(Sender:TObject; ACol: Integer; var DoSort: Boolean); 

 var fldname:string; 
 begin
 DoSort := False; // disable internal sort

 // toggle sort order if
 dbadvgrid1.SortSettings.Direction = sdAscending then
 dbadvgrid1.SortSettings.Direction := sdDescending else
 dbadvgrid1.SortSettings.Direction := sdAscending;

 // get field name of the column
 clicked fldname :=query1.FieldList.Fields[ACol -1].FieldName;

 if pos(' ',fldname)  0 then fldname:= 'biolife.db."'+fldname+'"';

 // add ORDER BY clause to the query
 query1.SQL.Text := 'select * from
 biolife.db ORDER BY '+fldname;

 if dbadvgrid1.SortSettings.Direction =
 sdDescending then query1.SQL.Text :=
 query1.SQL.Text + ' DESC';

 query1.Active := true;
 DBAdvGrid1.SortSettings.Column := ACol; 
 end;

if you want to order your clientdataset here you have how to do it: 如果要在此处订购clientdataset,请执行以下操作:

http://edn.embarcadero.com/article/29056 http://edn.embarcadero.com/article/29056

best regards, 最好的祝福,
Radu 拉杜

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM