Hey,

Is there an easy way with a TListView in report mode, to store a unique integer with each row? I've tried using the ".Data" property, but seem to end up with corruption quite often.

I'm trying to do this because I want to associate each row with a primary key ID from a database. At the moment I'm using a dynamically sized array, and adding the row id, to the db ID so I have to use, for example, rowsToIds[lv.SelectedIndex]; but it seems like a long-winded way to do it.

Is there an easier way to do this? Simple example of exactly what I'm trying to do...

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
  li: TListItem;
begin
  for i := 0 to 50 do begin
    li := lv1.Items.Add;
    li.Caption := 'xyz';
    li.SubItems.Add('abc');
    //Store a unique integer with the row here...
  end;
end;

procedure TForm1.lv1DblClick(Sender: TObject);
begin
  //Show the stored integer here...
end;

Thanks

unit IntObject;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
ComCtrls;

type
TForm1 = class(TForm)
ListView1: TListView;
procedure FormCreate(Sender: TObject);
procedure ListView1DblClick(Sender: TObject);
procedure ListView1SelectItem(Sender: TObject; Item: TListItem;
Selected: Boolean);
private
{ Private declarations }
aValue:Integer;
public
{ Public declarations }
end;

var
Form1 : TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var
i : Integer;
begin
for i := 0 to 50 do
begin

with ListView1.Items.Add do
begin
Caption := 'xyz';
SubItems.AddObject('abc', tobject(i));
end;
end;
end;
procedure TForm1.ListView1DblClick(Sender: TObject);
begin

if avalue>-1 then
showmessage(inttostr(aValue));
end;

procedure TForm1.ListView1SelectItem(Sender: TObject; Item: TListItem;
Selected: Boolean);
begin
if selected then
avalue:=Integer(Item.SubItems.Objects[0]) else
aValue:=-1;
end;

end.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.