ListView中的onData事件如何使用

解决方案 »

  1.   

    很简单,首先将listview属性OwnerData设为True
    然后在listview.ondata事件里利用Item: TListItem参数进行修改
    如 
     item.Caption:='Caption'+inttostr(item.index);
     item.SubItems.Add('subItems1'+inttostr(item.index));
     item.SubItems.Add('subItems2'+inttostr(item.index));
    最后在其他事件中要显示listview数据时,将
    ListView1.Items.Count:=100;即可
      

  2.   

    //listview属性OwnerData设为TRUEunit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ComCtrls;type  PLVRecord = ^TLVRecord;
      TLVRecord = packed record
      xh,zd1,zd2,zd3:string;
      end;  TForm1 = class(TForm)
        ListView1: TListView;
        procedure FormShow(Sender: TObject);
        procedure ListView1Data(Sender: TObject; Item: TListItem);
        procedure ListView1ColumnClick(Sender: TObject; Column: TListColumn);
      private
        { Private declarations }
        FList: TList;
        procedure InitData;
      public
        { Public declarations }
      end;  function CompareNames(Item1, Item2: Pointer): Integer;var
      Form1: TForm1;implementation{$R *.dfm}var
      iB:integer;function CompareNames(Item1, Item2: Pointer): Integer;
    begin
      case ib of
        0:Result := CompareValue(StrToInt(PlvRecord(Item1)^.xh),StrToInt(PlvRecord(Item2)^.xh));
        1:Result := Comparetext(PlvRecord(Item1)^.zd1,PlvRecord(Item2)^.zd1);
        2:Result := Comparetext(PlvRecord(Item1)^.zd2,PlvRecord(Item2)^.zd2);
        3:Result := Comparetext(PlvRecord(Item1)^.zd3,PlvRecord(Item2)^.zd3);
      end;
    end;procedure TForm1.InitData;
    var
      P: PLVRecord;
      i:integer
    begin
      with DM.Query_look do
      begin
        While not eof do
        begin      New(p);
          p^.xh:=IntToStr(i);
          p^.zd1:=FieldByName('zd1').AsString;
          p^.zd2:=FieldByName('zd2').AsString;
          p^.zd3:=FieldByName('zd3').AsString;
          flist.Add(p);
          i:=i+1;
          Next;
        end;
        listview1.Items.Count:=FList.Count;
        Close;
      end;
    end;procedure TForm1.FormShow(Sender: TObject);
    begin
      with dm.Query_look do
      begin
        Close;
        Sql.Clear;
        Sql.Add('select * from table1');
        Open;
        First;
        InitData;
      end;
    end;procedure TForm1.ListView1Data(Sender: TObject; Item: TListItem);
    begin
      if (Item.Index <= FList.Count) then
      begin
        item.Caption:=PlvRecord(FList.Items[item.index])^.xh;
        item.SubItems.Add(PlvRecord(FList.Items[item.index])^.zd1);
        item.SubItems.Add(PlvRecord(FList.Items[item.index])^.zd2);
        item.SubItems.Add(PlvRecord(FList.Items[item.index])^.zd3);
      end;
    end;procedure TForm1.ListView1ColumnClick(Sender: TObject;
      Column: TListColumn);
    begin
     listview1.Items.BeginUpdate;
     ib:=Column.Index;
     flist.Sort(@CompareNames);
     listview1.Items.EndUpdate;
    end;end.
      

  3.   

    delphi 6 中有例子virtuallistview.dpr
      

  4.   

    Delphi自带的Demos内就有了
    virtuallistview!
      

  5.   

    Occurs immediately before an item is displayed in the list view control.property OnData: TLVOwnerDataEvent;DescriptionUse OnData to customize an item before it is displayed in the list view control. Set the properties of the list item in the event handler so that they appear correctly when the item is drawn.OnData occurs only if OwnerData is True.