Ehlib3.0版本以上雖然支持排序功能,但不支持帶有Order By的SQL語句,而且排序很慢;我寫的這個排序函數,利用ADO的sort方法,排序很快,幾萬條數據也是很快。該函數支持Lookup字段排序,不支持計算字段排序,因為計算字段值在內存裡高速運算。排序分為:升序、降序和默認三種,支持排序圖標。
procedure SortDBGridEh(Sender: TObject; ACol: Integer;
Column: TColumnEh);
var
FIEldName, SortStr: string;
begin
Screen.Cursor := crSQLWait;
try
if (Sender is TDBGridEh) and
((Sender as TDBGridEh).DataSource.DataSet <> nil) then
begin
if not ((Sender as TDBGridEh).DataSource.DataSet is TCustomADODataSet) then
Exit;
if not (Sender as TDBGridEh).DataSource.DataSet.Active then
Exit;
FieldName := Column.FIEldName;
if (Sender as TDBGridEh).DataSource.DataSet.FindField(FIEldName).IsBlob then
Exit;
if (Sender as TDBGridEh).DataSource.DataSet.FieldByName(FieldName).FIEldKind =
fkData then
SortStr := FIEldName
else if (Sender as TDBGridEh).DataSource.DataSet.FieldByName(FieldName).FIEldKind =
fkLookup then
FieldName := (Sender as TDBGridEh).DataSource.DataSet.FieldByName(FieldName).KeyFIElds
else
FIEldName := '';
if (FieldName = '') or (Pos(';', FIEldName) > 0) then
Exit;
case Column.Title.SortMarker of
smNoneEh:
begin
Column.Title.SortMarker := smUpEh;
TCustomADODataSet((Sender as TDBGridEh).DataSource.DataSet).Sort :=
FIEldName;
end;
smUpEh:
begin
Column.Title.SortMarker := smDownEh;
TCustomADODataSet((Sender as TDBGridEh).DataSource.DataSet).Sort :=
FIEldName + ' DESC';
end;
smDownEh:
begin
Column.Title.SortMarker := smNoneEh;
TCustomADODataSet((Sender as TDBGridEh).DataSource.DataSet).Sort := '';
end;
end;
end;
finally
Screen.Cursor := crDefault;
end;
end;
--程序實現如下:
--在DBGridEh的事件OnTitleBtnClick引用該函數即可:
procedure TFrmU_BasicSetup.dbgAddrCodeTitleBtnClick(Sender: TObject;
ACol: Integer; Column: TColumnEh);
begin
SortDBGridEh(Sender, ACol, Column);
end;
--為了保證表格的每一列都能點擊觸發排序,你需要將你需要排序的列屬性
-- Title->TitleButton設置為True。