请看下面的代码:listview的columns属性加4个列项,viewstyle属性设为vsReport,smallimage属性对应imagelist。imagelist加载图标。query1对应与parts.db表。
这段代码能实现升序排列,降序你稍作改动即可。如果是按字段数据类型排序则可用sql语句实现。另外这段代码可实现响应双击第一列下的图标。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, ImgList, Db, DBTables;
type
TForm1 = class(TForm)
ListView1: TListView;
Button1: TButton;
ImageList1: TImageList;
Query1: TQuery;
procedure Button1Click(Sender: TObject);
procedure ListView1ColumnClick(Sender: TObject; Column: TListColumn);
procedure ListView1Compare(Sender: TObject; Item1, Item2: TListItem;
Data: Integer; var Compare: Integer);
procedure ListView1DblClick(Sender: TObject);
procedure ListView1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
mouseX, mouseY: Integer;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
var ColumnToSort: Integer;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
int:integer;
begin
listview1.Items.Clear;
int:=0;
with query1 do
begin
close;
sql.Clear;
sql.add(''select partno,description,onorder,cost from parts'');
prepare;
open;
first;
end;
while not query1.Eof do
begin
listView1.Items.Add.Caption:= query1.fieldbyname(''partno'').asstring;
ListView1.Items.Item[int].ImageIndex:=0;
ListView1.Items.Item[int].SubItems.Append( query1.fieldbyname(''description'').asstring);
ListView1.Items.Item[int].SubItems.Append( query1.fieldbyname(''onorder'').asstring);
ListView1.Items.Item[int].SubItems.Append( query1.fieldbyname(''cost'').asstring);
int:=int+1;
query1.Next;
end;
end;
procedure TForm1.ListView1ColumnClick(Sender: TObject;
Column: TListColumn);
begin
ColumnToSort := Column.Index;
(Sender as TCustomListView).AlphaSort;
end;
procedure TForm1.ListView1Compare(Sender: TObject; Item1, Item2: TLis