在DBGrid控件的 OnTitleclick 事件中添加代码……
DBGrid控件“点击标题栏自动排序功能”
关键词: DBGrid
procedure TForm1.DBGrid1TitleClick(Column: TColumn);
var
i,i:Integer;
cStr:string;
c:TColumn;
begin
for i:=0 to TDBGrid(Column.Grid).Columns.Count-1 do
begin
c:=TDBGrid(Column.Grid).Columns[i];
cStr:=c.Title.Caption;
if (pos('▲',cStr)=1) or (pos('▼',cStr)=1) then
begin
Delete(cStr,1,2);
c.Title.Caption:=cStr;
end;
end;
for i:= 1 to DBGrid1.Columns.Count do
begin
//恢复所有标题字体为默认
DBGrid1.Columns[i-1].Title.Font.Color := clWindowText;
DBGrid1.Columns[i-1].Title.Font.Style := [];
end;
if ADODataSet1.Sort<>(Column.FieldName+' ASC') then //判断原排序方式
begin
ADODataSet1.Sort := Column.FieldName+' ASC';
//Column.Title.Font.Color := clRed; //改变标题行字体为红色,表示当前的排序方式为升序
Column.Title.Font.Style := [fsBold];
Column.Title.Caption:='▲'+Column.Title.Caption; //标题加上三角形表示当前排序方式为升序;
end
else begin
ADODataSet1.Sort := Column.FieldName+' DESC';
//Column.Title.Font.Color := clBlue; //改变标题行字体为蓝色,表示当前的排序方式为降序
Column.Title.Font.Style := [fsBold];
Column.Title.Caption:='▼'+Column.Title.Caption; //标题加上倒三角形表示当前排序方式为降序;
end;
end;
来源:http://blns.
blns的博客