Storing an Integer with each row in a TListView?

Please support our Pascal and Delphi advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2009
Posts: 2
Reputation: janinesamson is an unknown quantity at this point 
Solved Threads: 0
janinesamson janinesamson is offline Offline
Newbie Poster

Storing an Integer with each row in a TListView?

 
0
  #1
Oct 18th, 2009
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...

Pascal and Delphi Syntax (Toggle Plain Text)
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3. i: Integer;
  4. li: TListItem;
  5. begin
  6. for i := 0 to 50 do begin
  7. li := lv1.Items.Add;
  8. li.Caption := 'xyz';
  9. li.SubItems.Add('abc');
  10. //Store a unique integer with the row here...
  11. end;
  12. end;
  13.  
  14. procedure TForm1.lv1DblClick(Sender: TObject);
  15. begin
  16. //Show the stored integer here...
  17. end;

Thanks
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 883
Reputation: pritaeas will become famous soon enough pritaeas will become famous soon enough 
Solved Threads: 144
Sponsor
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Practically a Posting Shark
 
0
  #2
Oct 19th, 2009
li.Tag perhaps. Not a very nice solution, but it works.
"If it is NOT source, it is NOT software."
-- NASA
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 10
Reputation: cao is an unknown quantity at this point 
Solved Threads: 4
cao cao is offline Offline
Newbie Poster
 
0
  #3
Oct 30th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 727 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Pascal and Delphi
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC