How do I move a Listview Column?

eNarc
eNarc used Ask the Experts™
on
Hi I'd like to programmly swap a listview column.

lets say Column 1 and I'd like to put column 1 where column 6 is and then put column 6 where 1 was.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Changed the viewstyle property to vsReport. Add columns via the Columns property.

Author

Commented:
Columns are made and data in columns, though how do I swap the columns without swapping the data just like you drag a column and move it while still keeping the column order even though now its moved.
Oh I see. I misread the question. Here's a procedure for that.
procedure SwapColumns(ListView: TListView; Column1Index, Column2Index: Integer);
var
  I : Integer;
  TempString : String;
  TempObject : Pointer;
  TempColumn : TListColumn;
begin
  //Bounds checking
  If Column1Index = Column2Index Then
    begin
    raise Exception.Create('Column indexes equal. Can''t swap!');
  end
  Else If (Column1Index < 0) Or (Column2Index < 0) Or
          (Column1Index >= ListView.Columns.Count) Or
          (Column2Index >= ListView.Columns.Count) Then
    begin
    raise Exception.Create('Column indexes are out of bounds!');
  end;
  ListView.Items.BeginUpdate;
  Try
    For I := 0 To ListView.Items.Count - 1 Do
      begin
      If Column1Index = 0 Then
        begin
        TempString := ListView.Items.Item[I].Caption;
        TempObject := ListView.Items.Item[I].Data;
        ListView.Items.Item[I].Caption := ListView.Items.Item[I].SubItems.Strings[Column2Index - 1];
        ListView.Items.Item[I].Data := ListView.Items.Item[I].SubItems.Objects[Column2Index - 1];
        ListView.Items.Item[I].SubItems.Strings[Column2Index - 1] := TempString;
        ListView.Items.Item[I].SubItems.Objects[Column2Index - 1] := TempObject;
      end
      Else
        begin
        TempString := ListView.Items.Item[I].SubItems.Strings[Column1Index - 1];
        TempObject := ListView.Items.Item[I].SubItems.Objects[Column1Index - 1];
        If Column2Index = 0 Then
          begin
          ListView.Items.Item[I].SubItems.Strings[Column1Index - 1] := ListView.Items.Item[I].Caption;
          ListView.Items.Item[I].SubItems.Objects[Column1Index - 1] := ListView.Items.Item[I].Data;
          ListView.Items.Item[I].Caption := TempString;
          ListView.Items.Item[I].Data := TempObject;
        end
        Else
          begin
          ListView.Items.Item[I].SubItems.Strings[Column1Index - 1] := ListView.Items.Item[I].SubItems.Strings[Column2Index - 1];
          ListView.Items.Item[I].SubItems.Objects[Column1Index - 1] := ListView.Items.Item[I].SubItems.Objects[Column2Index - 1];
          ListView.Items.Item[I].SubItems.Strings[Column2Index - 1] := TempString;
          ListView.Items.Item[I].SubItems.Objects[Column2Index - 1] := TempObject;
        end;
      end;
    end;
    TempColumn := TListColumn.Create(ListView.Columns);
    TempColumn.Assign(ListView.Column[Column1Index]);
    ListView.Column[Column1Index].Assign(ListView.Column[Column2Index]);
    ListView.Column[Column2Index].Assign(TempColumn);
    TempColumn.Free;
  Finally
    ListView.Items.EndUpdate;
  end;
end;

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial