Adding headers to TListView programmatically

Posted by on in UI

During today's Delphi, C++Builder and RAD Studio 10 Seattle launch webinar, I got a question on how to add headers to TListView programmatically instead of using LiveBindings with a data source.

Here is a quick code snippet showing how to programmatically add TListView headers.

procedure TListViewHeaders.FormCreate(Sender: TObject);
var
  Group, Item: Integer;
begin
  for Group in [1..4] do
  begin
    with ListView1.Items.Add do
    begin
      Text := Format('Header %d', [Group]);
      Purpose := TListItemPurpose.Header;
    end;
     for Item in [1..10] do
     ListView1.Items.Add.Text := Format('Regular item %d.%d', [Group, Item]);
  end;
end;

ListViewHeaders1

 



About
Gold User, Rank: 5, Points: 558
Senior Product Manager, RAD Studio

Comments

  • b.yacine
    b.yacine Sunday, 11 February 2018

    How do I delete duplicate values in listview ?

  • Jean-Luc M22328
    Jean-Luc M22328 Tuesday, 1 November 2016

    Hi Sarina,
    If I use "Purpose := TListItemPurpose.Header;" , how can I change the text font on Header ?

  • Sarina D
    Sarina D Tuesday, 1 November 2016

    Hi Jean-Luc,

    For Header items, you can change the font by selecting ListView1.ItemAppearance.Header.Text appearance in the Structure View, then changing the Font Settings in the Object Inspector.

    Regards,
    Sarina

  • Tuan-Loc
    Tuan-Loc Thursday, 14 July 2016

    hi Sarina,
    After setting the listview's Item Appearance to DynamicAppearance on my form. I have created the DynamicAppearance to contain: FLOC, Description, FLOCID (all of type TTextObjectAppearance). How can I now refer to those attributes to get their values on ItemClickEvent?

  • Tuan-Loc
    Tuan-Loc Thursday, 14 July 2016

    hi Sarina,
    After setting the listview's Item Appearance to DynamicAppearance on my form. I have created the DynamicAppearance to contain: FLOC, Description, FLOCID (all of type TTextObjectAppearance). How can I now refer to those attributes to get their values on ItemClickEvent?

  • Sarina D
    Sarina D Thursday, 14 July 2016

    Hi Tuan-Loc,
    The data displayed can be referred to by their names used as indices in the Data property, i.e. myItem.Data['Description'] := 'This item is important';

    Regards,
    Sarina

  • Tuan-Loc
    Tuan-Loc Thursday, 14 July 2016

    Thank you Sarina. That was super fast response.

  • Sarina D
    Sarina D Thursday, 14 July 2016

    Hi Tuan-Loc,
    Glad I was able to help.

    Regards,
    Sarina

  • Alfan A10135
    Alfan A10135 Wednesday, 16 March 2016

    If add Header using this method, when I change the headerObeject font style to bold is not working, unless I change in the ItemObject then the item text is bold too.

  • Alfan
    Alfan Thursday, 3 September 2015

    Hello,

    I doqnload DX trials and try to compile toiOS but always hang after splashscreen, at android is running ok. I try to compile to iOS simple app with just 1 button and have the same effect.
    is there's a problem with iOS

    Thank you

  • Please login first in order for you to submit comments
  • Page :
  • 1

Check out more tips and tricks in this development video: