你把DBGrid换成ListView来写,写几句话就可以排序了
ListView1.OnCompare event
procedure TForm1.ListView1Compare(Sender: TObject;
Item1,
Item2: TListItem;
Data: Integer;
var Compare: Integer);
begin
//Data就是CustomSort的参数,你可以传个指针过来,转成自己所用的信息,然后
//区别出是该上/下排
if Data = -1 then
Compare := AnsiCompareText(Item1.Caption, Item2.Caption) else
Compare := AnsiCompareText(Item1.SubItems[Data], Item2.SubItems[Data]);
end;
//将Columns.Imageindex加入一些像上/下箭头
ListView1 ColumnClick event
procedure TForm1.ListView1ColumnClick(Sender: TObject;
Column: TListColumn);
begin
ListView1.CustomSort(nil, Column.Index - 1);
end;