1、建立私有变量,控制排序方向与排序字段
private
FSort, FSortField: String; //控制Grid排序
2、在titleClick中写排序代码
procedure TfrmuZhutiZhiBiaoDataInput.DBGridEhInputTitleClick(
Column: TColumnEh);
var
iFieldCount:Integer;
begin
//进行点击Title排序
for iFieldCount := 0 to DBGridEhInput.Columns.Count - 1 do
begin
if (Copy(DBGridEhInput.Columns[iFieldCount].Title.Caption,Length(DBGridEhInput.Columns[iFieldCount].Title.Caption)-1,2) = '▼') or (Copy(DBGridEhInput.Columns[iFieldCount].Title.Caption,Length(DBGridEhInput.Columns[iFieldCount].Title.Caption)-1,2) = '▲') then
begin
DBGridEhInput.Columns[iFieldCount].Title.Caption := Copy(DBGridEhInput.Columns[iFieldCount].Title.Caption,1,Length(DBGridEhInput.Columns[iFieldCount].Title.Caption)-3);
break;
end;
end;
if Column.FieldName = FSortField then
begin
if FSort = 'DESC' then
FSort := 'ASC'
else
FSort := 'DESC';
end
else begin
FSortField := Column.FieldName;
FSort := 'ASC';
end;
if FSort = 'ASC' then
Column.Title.Caption := Column.Title.Caption + ' ▲'
else
Column.Title.Caption := Column.Title.Caption + ' ▼';
AQDataInput.Sort := Column.FieldName + ' ' + FSort;
end;
二、ListView点击标题栏自动排序
1、定义全局变量
var m_bSort: boolean = False; //控制正反排序的变量
2、在程序代码最上方写上排序函数
//ListView排序的回调函数,默认的是快速排序法,也可以自己在这里做算法
function CustomSortProc(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall;
var
txt1, txt2: string;
aInt: Integer;
begin
Result := 0;
if ParamSort <> 0 then
begin
try
txt1 := Item1.SubItems.Strings[ParamSort-1];
txt2 := Item2.SubItems.Strings[ParamSort-1];
if TryStrToInt(txt1, aInt) and TryStrToInt(txt2, aInt) then
begin
if m_bSort then
begin
if StrToInt(txt1) > StrToInt(txt2) then
begin
Result := 1;
end else if StrToInt(txt1) = StrToInt(txt2) then
begin
Result := 0;
end else
begin
Result := -1;
end;
end else
begin
if StrToInt(txt1) > StrToInt(txt2) then
begin
Result := -1;
end else if StrToInt(txt1) = StrToInt(txt2) then
begin
Result := 0;
end else
begin
Result := 1;
end;
end;
end else
begin
if m_bSort then
begin
Result := CompareText(txt1, txt2);
end else begin
Result := -CompareText(txt1, txt2);
end;
end;
except
end;
end else
begin
if TryStrToInt(Item1.Caption, aInt) and TryStrToInt(Item2.Caption, aInt) then
![DBGrid及ListView点击标题栏自动排序 listview 点击排序](http://img.413yy.cn/images/31101031/31065227t015a177f25c7881530.png)
begin
if m_bSort then
begin
if StrToInt(Item1.Caption) > StrToInt(Item2.Caption) then
begin
Result := 1;
end else if StrToInt(Item1.Caption) = StrToInt(Item2.Caption) then
begin
Result := 0;
end else
begin
Result := -1;
end;
end else
begin
if StrToInt(Item1.Caption) > StrToInt(Item2.Caption) then
begin
Result := -1;
end else if StrToInt(Item1.Caption) = StrToInt(Item2.Caption) then
begin
Result := 0;
end else
begin
Result := 1;
end;
end;
end else
begin
if m_bSort then
begin
Result := CompareText(Item1.Caption,Item2.Caption);
end else
begin
Result := - CompareText(Item1.Caption,Item2.Caption);
end;
end;
end;
end;
3、在ColumnClick中增加排序调用的代码
procedure TframeDaDuiSort.ListViewDaDuiColumnClick(Sender: TObject;
Column: TListColumn);
begin
ListViewDaDui.CustomSort(@CustomSortProc, Column.Index);
m_bSort := not m_bSort;
end;