Add LISTVIEW column

Asked

Viewed 1,048 times

1

I have a listview component in my project, and it contains a Column with several values inserted. Does anyone have any idea how I can add an entire column of listview that only contains values like:

 300,00
 52,00
 100,00

I appreciate any help....

  • Do not add in listview. Add when inserting in listview from the variables that populate the items in the loop, then you just add the line of the total if you want to display on the screen (or use the value elsewhere in the software as you wish).

1 answer

2


Follows part of the source code of a form I created to test the solution.

type
  TfrmPrincipal = class(TForm)
    lvLista: TListView;
    btnSoma: TButton;
    procedure btnSomaClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmPrincipal: TfrmPrincipal;

implementation

{$R *.dfm}

procedure TfrmPrincipal.btnSomaClick(Sender: TObject);
var
  dSoma : double;
  i : integer;
begin
  dSoma := 0;
  for i := 0 to MyListView.Items.Count - 1 do begin
    dSoma := dSoma + StrToFloatDef( MyListView.Items[ i ].Caption, 0 );
  end;
  ShowMessage( FloatToStr( dSoma ) );
end;

Tested in Delphi7.

". Caption" accesses the value of the first column. If it is necessary to access others, it should be used ". Subitems[x]", remembering that "x" is equal to "column number" minus 2.

Examples:

  • The second column of Listview is the first sub-iten and this has index "0" (zero)
  • The seventh column of Listview would be accessed with ". Subitems[5]"
  • Thanks @Anselmo, I need to add a certain SUBITEM, I tried this way did not work, what I did wrong: dSoma := dSoma + Strtofloatdef( LV.Items[ i ].Subitems[7], 0 );

  • The syntax of your command is correct. The only error I can imagine is in the index of sub-items, in your case, "7". The "Subitems" begin to be counted from the second column and begin with index "0" (zero). So to access, for example, the seventh column you would need to use LV.Items[ i ].SubItems[5]

  • Perfect @Anselmo, thanks!

  • Wow! Perfect! Brilliant explanation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.