我是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),我只需要在内存中对数据进行排序,而不需要对数据库进行另一次查询。
谢谢
我试过你的例子,这为我解决了这个问题:
Grid.PageMode := False;
为了解决这个问题,您必须对网格后面的数据集进行排序。这里有一般的操作方法: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最诚挚的问候,拉杜