我主要的意思是:使用ListView的ONDATA事件加快listview的加载速度后,原先ColumnClick事件写的的排序功能就失效了,请问如何解决。源码如下:加快加载速度:unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, DB, DBTables;type PMyRecord = ^TMyRecord; TMyRecord = packed record ID,xm,xb,gh:string; end; TForm1 = class(TForm) Database1: TDatabase; Query1: TQuery; ListView1: TListView; procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure ListView1Data(Sender: TObject; Item: TListItem); procedure FormCreate(Sender: TObject); private { Private declarations } FList: TList; procedure InitData(AList: TList); public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.InitData(AList: TList); var P: PMyRecord; begin with query1 do begin Close; Sql.Clear; Sql.Add('select id,gh,xm,xb from ryjbxxb order by id'); Open; First; While not eof do begin New(p); p^.ID:=FieldByName('id').AsString; p^.gh:=FieldByName('gh').AsString; p^.xm:=FieldByName('xm').AsString; p^.xb:=FieldByName('xb').AsString; AList.Add(p); Next; end; Close; end; end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); var j:integer; begin for j:=0 to FList.Count-1 do DisPose(PMyRecord(FList.Items[j])); end;procedure TForm1.ListView1Data(Sender: TObject; Item: TListItem); begin if (Item.Index > FList.Count) then Exit; item.Caption:=PMyRecord(FList.Items[item.index])^.id; item.SubItems.Add(PMyRecord(FList.Items[item.index])^.gh); item.SubItems.Add(PMyRecord(FList.Items[item.index])^.xm); item.SubItems.Add(PMyRecord(FList.Items[item.index])^.xb); end;procedure TForm1.FormCreate(Sender: TObject); begin FList := TList.Create; InitData(FList); ListView1.Items.Count:=FList.Count; end;procedure TForm1.ListView1ColumnClick(Sender: TObject; Column: TListColumn); var SortStyle: TSortStyle; begin case Column.Index of 0: SortStyle := ssNumeric; 1,2,3: SortStyle := ssAlpha; end; SortListView(Lv_htbm, Column.Index, SortStyle, true); end;end. procedure TForm1.ListView1ColumnClick(Sender: TObject; Column: TListColumn); var SortStyle: TSortStyle; begin case Column.Index of 0: SortStyle := ssNumeric; 1,2,3,4,5,6: SortStyle := ssAlpha; end; SortListView(Lv_htbm, Column.Index, SortStyle, true); end;排序代码 ... ... type TSortStyle = (ssAlpha, ssNumeric, ssDateTime); PSortInfo = ^TSortInfo; TSortInfo = record Col: Integer; Style: TSortStyle; Asc: Boolean; end; .... ... function ListViewCompare(I1, I2: TListItem; Data: Integer): Integer; stdcall; var V1, V2: string; function Sign(Val: Extended): Integer; begin if Val < 0 then Result := -1 else if Val > 0 then Result := 1 else Result := 0; end; //去除非数字字符,自己重写 function ExtractNum(const S: string): string; var i: Integer; begin Result := ''; for i := Length(S) downto 1 do begin if S[i] in ['0'..'9'] then Result := S[i] + Result; end; end;begin with PSortInfo(Data)^ do begin if Col = 0 then begin V1 := I1.Caption; V2 := I2.Caption; end else begin V1 := I1.SubItems[Col - 1]; V2 := I2.SubItems[Col - 1]; end; case Style of ssAlpha: Result := AnsiCompareText(V1, V2); ssNumeric: Result := Sign(StrToFloat(ExtractNum(V1)) - StrToFloat(ExtractNum(V2))); ssDateTime: Result := Sign(StrToDateTime(V1) - StrToDateTime(V2)); else Result := 0; end; if not Asc then Result := -Result; end; end;//== ListView 排序 ============================================================= procedure SortListView(ListView: TTEListView; ColumnIndex: Integer; Style: TSortStyle; Ascending: Boolean = True); // 参数说明 // ColumnIndex: 排序列索引号, // Style : 排序方式;(按字符,按数值,按日期) // (日期格式为SysUtils.ShortDataTimeFmt,缺省为YY-MM-DD); // Ascending : = True 按升序,否则按降序} var FSortInfo: TSortInfo; begin FSortInfo.Col := ColumnIndex; FSortInfo.Style := Style; FSortInfo.Asc := Ascending; ListView.CustomSort(@ListViewCompare, LongInt(@FSortInfo)); end;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, DB, DBTables;type
PMyRecord = ^TMyRecord;
TMyRecord = packed record
ID,xm,xb,gh:string;
end; TForm1 = class(TForm)
Database1: TDatabase;
Query1: TQuery;
ListView1: TListView;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure ListView1Data(Sender: TObject; Item: TListItem);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
FList: TList;
procedure InitData(AList: TList);
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.InitData(AList: TList);
var
P: PMyRecord;
begin with query1 do
begin
Close;
Sql.Clear;
Sql.Add('select id,gh,xm,xb from ryjbxxb order by id');
Open;
First;
While not eof do
begin
New(p);
p^.ID:=FieldByName('id').AsString;
p^.gh:=FieldByName('gh').AsString;
p^.xm:=FieldByName('xm').AsString;
p^.xb:=FieldByName('xb').AsString;
AList.Add(p);
Next;
end;
Close;
end;
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
j:integer;
begin
for j:=0 to FList.Count-1 do
DisPose(PMyRecord(FList.Items[j]));
end;procedure TForm1.ListView1Data(Sender: TObject; Item: TListItem);
begin
if (Item.Index > FList.Count) then Exit;
item.Caption:=PMyRecord(FList.Items[item.index])^.id;
item.SubItems.Add(PMyRecord(FList.Items[item.index])^.gh);
item.SubItems.Add(PMyRecord(FList.Items[item.index])^.xm);
item.SubItems.Add(PMyRecord(FList.Items[item.index])^.xb);
end;procedure TForm1.FormCreate(Sender: TObject);
begin
FList := TList.Create;
InitData(FList);
ListView1.Items.Count:=FList.Count;
end;procedure TForm1.ListView1ColumnClick(Sender: TObject;
Column: TListColumn);
var
SortStyle: TSortStyle;
begin
case Column.Index of
0:
SortStyle := ssNumeric;
1,2,3:
SortStyle := ssAlpha;
end;
SortListView(Lv_htbm, Column.Index, SortStyle, true);
end;end.
procedure TForm1.ListView1ColumnClick(Sender: TObject;
Column: TListColumn);
var
SortStyle: TSortStyle;
begin
case Column.Index of
0:
SortStyle := ssNumeric;
1,2,3,4,5,6:
SortStyle := ssAlpha;
end;
SortListView(Lv_htbm, Column.Index, SortStyle, true);
end;排序代码
...
...
type TSortStyle = (ssAlpha, ssNumeric, ssDateTime);
PSortInfo = ^TSortInfo;
TSortInfo = record
Col: Integer;
Style: TSortStyle;
Asc: Boolean;
end;
....
...
function ListViewCompare(I1, I2: TListItem; Data: Integer): Integer; stdcall;
var
V1, V2: string; function Sign(Val: Extended): Integer;
begin
if Val < 0 then
Result := -1
else if Val > 0 then
Result := 1
else
Result := 0;
end; //去除非数字字符,自己重写
function ExtractNum(const S: string): string;
var
i: Integer;
begin
Result := '';
for i := Length(S) downto 1 do
begin
if S[i] in ['0'..'9'] then
Result := S[i] + Result;
end;
end;begin
with PSortInfo(Data)^ do
begin if Col = 0 then
begin
V1 := I1.Caption;
V2 := I2.Caption;
end
else
begin
V1 := I1.SubItems[Col - 1];
V2 := I2.SubItems[Col - 1];
end; case Style of
ssAlpha: Result := AnsiCompareText(V1, V2);
ssNumeric: Result := Sign(StrToFloat(ExtractNum(V1)) - StrToFloat(ExtractNum(V2)));
ssDateTime: Result := Sign(StrToDateTime(V1) - StrToDateTime(V2));
else
Result := 0;
end; if not Asc then
Result := -Result;
end;
end;//== ListView 排序 =============================================================
procedure SortListView(ListView: TTEListView; ColumnIndex: Integer;
Style: TSortStyle; Ascending: Boolean = True);
// 参数说明
// ColumnIndex: 排序列索引号,
// Style : 排序方式;(按字符,按数值,按日期)
// (日期格式为SysUtils.ShortDataTimeFmt,缺省为YY-MM-DD);
// Ascending : = True 按升序,否则按降序}
var
FSortInfo: TSortInfo;
begin
FSortInfo.Col := ColumnIndex;
FSortInfo.Style := Style;
FSortInfo.Asc := Ascending;
ListView.CustomSort(@ListViewCompare, LongInt(@FSortInfo));
end;
你所说的先排完序是什么意思?取数据时SQL里面order by???