[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 424
  • Last Modified:

listview columns layout / save layout and load it as well

Hi Experts

I have a listview with 6 static columns.
I want to add dynamic columns during runtime and then to save the new layout as the user name and then to call it again by clicking on the user name. so what i need is to customize the listview up to each user using the application

hope i made my self clear.

one more thing how can i export the listview items to an excel and vice versa

thanks all in advance
0
ibrobar
Asked:
ibrobar
  • 3
  • 2
  • 2
1 Solution
 
mokuleCommented:
Hi
For Excel automation look there
http://www.djpate.freeserve.co.uk/AutoExcl.htm
0
 
mokuleCommented:
Store appropriate parameters in an INI file. For example sections for each user.
TIniFile
0
 
rafamvcCommented:
"I want to add dynamic columns during runtime"

ListView1.Columns.Add;

" and then to save the new layout as the user name"
save to a file? db?

Maybe this help:
procedure TForm1.Button1Click(Sender: TObject);
var i,j: Integer;
    TempColumn : TListColumn;
begin
   //TempColumn := ListView1.Columns.Add;
   //TempColumn.Caption := 'New Column';
   //ListView1.Columns.Count;
   for i:=0 to ListView1.Columns.Count -1 do
   begin
     //Save where you want the columns captions
     ShowMessage(ListView1.Column[i].Caption);
   end;
   for i:=0 to ListView1.Items.Count -1 do
   begin
     ShowMessage(ListView1.Items.Item[i].Caption);
     for j:=0 to ListView1.Columns.Count -2 do
     //Caution: -2 because first column is the item itself
        ShowMessage(ListView1.Items.Item[i].SubItems[j]);
   end;
end;

"and then to call it again by clicking on the user name."
Explain a little more about where you will save and then we make the second part

" so what i need is to customize the listview up to each user using the application"
ok.

[]´s
Rafael Cardoso - Brazil
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
rafamvcCommented:
  //TempColumn := ListView1.Columns.Add;
   //TempColumn.Caption := 'New Column';
   //ListView1.Columns.Count;

ops forgot to explain:

this 3 lines set up new column.
0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
Ok my version:

To save and reload the listviews you could use the readcomponent and writecomponent procedures that are really useful...

To export to excel create a oleobject and fill it reading the listview

my example as follows:

{unit1.pas}

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    ListView1: TListView;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  MemStr : TMemoryStream;
begin
  MemStr := TMemoryStream.Create;
  try
    {if you need to save just the structure without the content simply add
    ListView1.Items.Clear}
    MemStr.WriteComponent(ListView1);
    memSTr.SaveToFile('c:\ThisUserListView.dat');
  finally
    MemStr.Free;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
MemStr : TMemoryStream;
begin
MemStr:= TMemoryStream.Create;
  try
    MemStr.LoadFromFile('c:\ThisUserListView.dat');
    MemStr.ReadComponent(ListView1);
  finally
    MemStr.Free;
  end;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
Excel: Variant;
row,col:Integer;
begin
  try
    excel:=CreateOleObject('Excel.Application');
  except
    ShowMEssage('Excel not available');
    exit;
  end;
    excel.Visible:=False;
    Row:=1;
    excel.Workbooks.Add;
    for Col := 0 to ListView1.Columns.Count-1 do begin
      excel.Cells[row,Col + 1].Value:= ListView1.Columns.Items[col].Caption;
      excel.cells[row,Col+1].ColumnWidth := ListView1.Columns.Items[col].Width/5;
      end;
    try
    for row := 2 to ListView1.items.count+1 do
      for Col := 0 to ListView1.Columns.Count-1 do begin
        If Col = 0 then
          excel.Cells[row,Col + 1].Value:= (ListView1.Items[Row-2].caption)
        else
          excel.Cells[row,Col + 1].Value:= (ListView1.Items[Row-2].subitems[col-1]);
    end;
    Except
         MessageBox(0,'Warning, there was an error during the operation. Exportation aborted.','',0);
         excel.quit;
    end;
    excel.visible := true;
    excel := Unassigned;
end;

end.

{Unit1.dfm}

object Form1: TForm1
  Left = 192
  Top = 107
  Width = 507
  Height = 291
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object ListView1: TListView
    Left = 130
    Top = 72
    Width = 250
    Height = 150
    Columns = <
      item
        Caption = 'Title1'
      end
      item
        Caption = 'Title2'
      end
      item
        Caption = 'Title3'
      end
      item
        Caption = 'Title4'
      end>
    Items.Data = {
      910000000400000000000000FFFFFFFFFFFFFFFF030000000000000001310132
      0133013400000000FFFFFFFFFFFFFFFF03000000000000000132013501360137
      00000000FFFFFFFFFFFFFFFF0300000000000000013301380139023130000000
      00FFFFFFFFFFFFFFFF03000000000000000134013101320133FFFFFFFFFFFFFF
      FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF}
    TabOrder = 0
    ViewStyle = vsReport
  end
  object Button1: TButton
    Left = 138
    Top = 20
    Width = 75
    Height = 25
    Caption = 'Save List'
    TabOrder = 1
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 292
    Top = 20
    Width = 75
    Height = 25
    Caption = 'Load List'
    TabOrder = 2
    OnClick = Button2Click
  end
  object Button3: TButton
    Left = 390
    Top = 194
    Width = 75
    Height = 25
    Caption = 'To Excel'
    TabOrder = 3
    OnClick = Button3Click
  end
end
0
 
rafamvcCommented:
wow, realy nice your code! ;) Solved another problem of mine ;)
tnks Ferruccio68!
[]´s
Rafael Cardoso
0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
Rafael, just for curiosity: what was your problem? About the excel export or the component streaming? :)))
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now