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

发布于 2024-11-02 02:36:57 字数 840 浏览 7 评论 0原文

我是 Delphi 的新手,我需要建议。

我正在使用 TMS TDBAdvGrid,当用户单击列标题时,我需要对数据进行排序。我设置了网格的排序设置,并为 onclicksort 事件编写了代码,但它不起作用。

网格的排序设置:

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

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;

网格不直接链接到数据库。数据被加载到内存(TClientDataSet)中,我只需要在内存中对数据进行排序,而不需要再次查询数据库。

谢谢

I'm a newbie into Delphi and i need an advice.

I'm using a TMS TDBAdvGrid and i need to sort the data when the user is clicking the header of a column. I setup the sort settings of the grid and i write code for the onclicksort event, but it is not working.

The sort settings of the grid:

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

the onclicksort event:

 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.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

分開簡單 2024-11-09 02:36:57

我尝试了你的例子,这为我解决了这个问题:

Grid.PageMode := False;

I tried your example and this solved the issue for me:

Grid.PageMode := False;
水溶 2024-11-09 02:36:57

为了解决此问题,您必须对网格后面的数据集进行排序。这里有一般如何执行此操作的信息:http://delphi.about.com/od/usedbvcl/l/aa042203a.htm。

下面有一个示例:

 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;

如果您想在此处订购 clientdataset,您可以这样做:

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

致以诚挚的问候,
拉杜

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.

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:

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

best regards,
Radu

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文