如何对DBGRIRD中数据进行排序?(急)(30分)

  • 主题发起人 主题发起人 dzb110
  • 开始时间 开始时间
D

dzb110

Unregistered / Unconfirmed
GUEST, unregistred user!
如何在DBGRIRD点击不同的字段进行相应的排序?请指教,谢谢!
 
procedure TForm4.DBGrid1TitleClick(Column: TColumn);
begin

with ryADOTable1 do
begin
if DBGrid1Boolean then
TADOTable(ryADOTable1).Sort := Column.FieldName + ' DESC'
else
TADOTable(ryADOTable1).Sort := Column.FieldName;
DBGrid1Boolean := not(DBGrid1Boolean);
end;
end;
 
如果是TABLE或QUERY又该如何?
 
还要注意对不能排序的数据类型(如text)的屏蔽
最简单就是
try
lcl_003的代码
except
end;
在运行时候是不会报错的
 
dbgrideh(ehlib)
 
好象TABLE没有SORT这个方法
 
如果dbgrid连接的是query,那么需要在点击DBgrid的列标题时重新进行数据查找,
并按照列标题进行排序,例如:
procedure TForm1.DBGrid1TitleClick(Column: TColumn);
begin
with query1 do
begin
close;
sql.Clear;
sql.Add('select * from data order by :orderlaber');
parambyname('orderlaber').value:=column.fieldname;
open;
end;
end;
 
单击网格标题排序(转帖)
procedure TForm1.GridTitleSort(Column: TColumn);
var s,cFieldName:string;
i:integer;
DataSet:TDataSet;
procedure setTitle;
var ii:integer;
cStr:string;
c:TColumn;
begin
for ii:=0 to TDBGrid(Column.Grid).Columns.Count-1 do
begin
c:=TDBGrid(Column.Grid).Columns[ii];
cStr:=c.Title.Caption;
if (pos('▲',cStr)=1) or (pos('▼',cStr)=1) then
begin
Delete(cStr,1,2);
c.Title.Caption:=cStr;
end;
end;

end;
begin
setTitle;
DataSet:=Column.Grid.DataSource.DataSet;
if Column.Field.FieldKind=fkLookup then
cFieldName:=Column.Field.KeyFields
else if Column.Field.FieldKind=fkCalculated then
cFieldName:=Column.Field.KeyFields
else
cFieldName:=Column.FieldName;
//=================================AdoDataSet=====================

if DataSet is TCustomADODataSet then begin
s:=TCustomADODataSet(DataSet).Sort;
if s='' then begin
s:=cFieldName;
Column.Title.Caption:='▲'+Column.Title.Caption;
end
else begin
if Pos(cFieldName,s)<>0 then begin
i:=Pos('DESC',s);
if i<=0 then begin
s:=s+' DESC';
Column.Title.Caption:='▼'+Column.Title.Caption;
end
else begin
Column.Title.Caption:='▲'+Column.Title.Caption;
Delete(s,i,4);
end;
end
else begin
s:=cFieldName;
Column.Title.Caption:='▲'+Column.Title.Caption;
end;
end;
TCustomADODataSet(DataSet).Sort:=s;
end
//============================Clientdataset==========================
else if DataSet is TClientDataSet then begin
if TClientDataSet(DataSet).indexfieldnames<>'' then
begin
i:=TClientDataSet(DataSet).IndexDefs.IndexOf('i'+Column.FieldName);
if i=-1 then
begin
with TClientDataSet(DataSet).IndexDefs.AddIndexDef do
begin
Name:='i'+Column.FieldName;
Fields:=Column.FieldName;
DescFields:=Column.FieldName;
end;
end;
TClientDataSet(DataSet).IndexFieldNames:='';
TClientDataSet(DataSet).IndexName:='i'+Column.FieldName;
Column.Title.Caption:='▼'+Column.Title.Caption;
end
else
begin
TClientDataSet(DataSet).IndexName:='';
TClientDataSet(DataSet).IndexFieldNames:=column.fieldname;
Column.Title.Caption:='▲'+Column.Title.Caption;
end;
end;
end;
 
procedure TfrmMainForm.DBG_Proj_ListTitleClick(Column: TColumn);
begin
try
if TADOTable(Column.Grid.DataSource.DataSet).Active then
if Pos('DESC',TADOTable(Column.Grid.DataSource.DataSet).Sort)=0 then
TADOTable(Column.Grid.DataSource.DataSet).Sort :=Column.FieldName + ' DESC'
else
TADOTable(Column.Grid.DataSource.DataSet).Sort :=Column.FieldName;
except
end;
end;
 
wab说的方法实在是好,我试了,完全可以。
可以结贴了。
 
后退
顶部