dbgrideh 如何实现点击标题后按标题字段排序 ( 积分: 20 )

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

delphijob

Unregistered / Unconfirmed
GUEST, unregistred user!
dbgrideh 如何实现点击标题后按标题字段排序
 
这是本地数据集排序:
procedure SortGrid(DBGrid: TDBGrid; Column: TColumn);
var
ADataSet: TADOQuery;
i : integer;
begin
if DBGrid.DataSource = nil then
Exit;
if DBGrid.DataSource.DataSet = nil then
Exit;
if DBGrid.DataSource.DataSet.Active = False then
Exit;
if DBGrid.DataSource.DataSet is TADOQuery then
ADataSet := TADOQuery(DBGrid.DataSource.DataSet)
else
Exit;

if not (Column.Field.FieldKind in [fkData]) then
Exit;

for i:= 1 to DBGrid.Columns.Count do
begin
//恢复所有标题字体为默认
DBGrid.Columns[i-1].Title.Font.Style := [];
if pos(' ',DBGrid.Columns[i-1].Title.Caption) > 0 then
DBGrid.Columns[i-1].Title.Caption :=copy(DBGrid.Columns[i-1].Title.Caption,1,pos(' ',DBGrid.Columns[i-1].Title.Caption)-1);
end;
if ADataSet.Sort<>(Column.FieldName+' ASC') then //判断原排序方式
begin
ADataSet.Sort := Column.FieldName+' ASC';
if pos(' ',Column.Title.Caption)= 0 then
Column.Title.Caption :=Column.Title.Caption+ ' ▲'
else
Column.Title.Caption :=copy(Column.Title.Caption,1,pos(' ',Column.Title.Caption)-1)+' ▲';
// Column.Title.Font.Style := [fsBold];
end
else begin
ADataSet.Sort := Column.FieldName+' DESC';
if pos(' ',Column.Title.Caption)= 0 then
Column.Title.Caption :=Column.Title.Caption+ ' ▼'
else
Column.Title.Caption :=copy(Column.Title.Caption,1,pos(' ',Column.Title.Caption)-1)+' ▼';
// Column.Title.Font.Style := [fsBold];
end;
end;
 
使用cxgrid控件,到盒子下载.
 
3、自动显示标题行的升降排序标志符(▽降序△升序)并做相应排序
DBGridEh组件可以在标题行单元格中显示小三角形升、降排序标志符图片,在运行时可点击标题行,图片自动切换并做相应排序。具体属性设置如下:

OptionsEh=dghAutoSortMarking
Column.Title.TitleButton=true

SortMarkedColumns 为当前排序列可在运行时使用.
然后在该列的ontitleclick事件中添加代码:
procedure TForm_Query.DBGridEh1TitlebtnClick(Sender: TObject; ACol: Integer; Column: TColumnEh);
var
sortstring:string; //排序列
begin
//进行排序
with Column do
begin
if FieldName = '' then
Exit;
case Title.SortMarker of
smNoneEh:
begin
Title.SortMarker := smDownEh;
sortstring := Column.FieldName + ' ASC';
end;
smDownEh: sortstring:= Column.FieldName + ' ASC';
smUpEh: sortstring := Column.FieldName + ' DESC';
end;
//进行排序
try
ADOQuery1.Sort := sortstring //dataset为实际数据集变量名
except
end;
end;
end;
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部