For Newbie

Tuesday, 15 November 2011

Read a String in Sub-Item ListView (ViewStyle = vsReport) by Delphi


Have you ever experienced problems when trying to read the data sub-item (column) specific to the TListView. Not a difficult thing indeed but not least the beginners who are having problems. Here is the solution.

Function declaration (to be more efficient to use):
DataItemListView function (LV: TListView; Row, Col: Integer): String;
begin
   Result: ='';
   if Col> LV.Columns.Count then Exit;
   do with LV
   begin
     if (Col <= 1) then
       Result: = Items.Item [Row]. Caption
     else
       Result: = Items.Item [Row]. Subitems [Col - 2];
   end;
end;

parameters:
LV: TListView you use.
Row: Index line item, index starts from 0.
Col: the column index item, index starts from 1.

Example of implementation:
TForm1.ListView1SelectItem procedure (Sender: TObject; Item: TListItem;
   Selected: Boolean);
begin
   with ListView1 do
   begin
     if Selected = nil then Exit;
     Edit1.Text: = DataItemListView (ListView1, Selected.Index, 1);
     Edit2.Text: = DataItemListView (ListView1, Selected.Index, 2);
     Edit3.Text: = DataItemListView (ListView1, Selected.Index, 3);
   end;
end;

Share on Facebook
Share on Twitter
Share on Google+

Related : Read a String in Sub-Item ListView (ViewStyle = vsReport) by Delphi