一、DBGrid點擊標(biāo)題欄自動排序
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點擊標(biāo)題欄自動排序
1、定義全局變量
var m_bSort: boolean = False; //控制正反排序的變量
2、在程序代碼最上方寫上排序函數(shù)
//ListView排序的回調(diào)函數(shù),默認的是快速排序法,也可以自己在這里做算法
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
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中增加排序調(diào)用的代碼
procedure TframeDaDuiSort.ListViewDaDuiColumnClick(Sender: TObject;
Column: TListColumn);
begin
ListViewDaDui.CustomSort(@CustomSortProc, Column.Index);
m_bSort := not m_bSort;
end;