Grid column order retrieval

Hi,

I'm using a TDBAdvGrid and trying to allow users to reorder and resize the columns. The column order and size is stored in the program database.

Loading the column data seems to be working:


var
  ColList: TStringList;
  x, ColPos, ColWidth: Integer;
  ColName: String;
begin
// first hide all columns
for x := 1 to DBAdvGrid1.Columns.Count - 1 do
  DBAdvGrid1.HideColumn(x);

// now restore columns if listed in ColList
ColPos := 1;
while ColList.Count > 0 do
begin
  // cut out code to retrieve column name & width from ColList, stored in ColName and ColWidth
  for x := 1 to DBAdvGrid1.Columns.Count - 1 do
      if UpperCase(DBAdvGrid1.Columns.Items[x].Header) = UpperCase(ColName) then
      begin
        DBAdvGrid1.UnHideColumn(x);
        DBAdvGrid1.Columns.Items[x].Width := ColWidth;
        DBAdvGrid1.Columns.Items[x].Index := ColPos;
        Inc(ColPos);
      end;
  ColList.Delete(0);
end;
end;



However when I try to retrieve the columns for storage I get some strange behaviour:


var
  ColStr: String;
begin
for x := 1 to DBAdvGrid1.Columns.Count - 1 do
    if not (DBAdvGrid1.IsHiddenColumn(x)) and (DBAdvGrid1.Columns.Items[x].Header <> '') then
      ColStr := ',' + DBAdvGrid1.Columns.Items[x].Header + '|' + IntToStr(DBAdvGrid1.Columns.Items[x].Width) + ColStr;
ColStr := Copy(ColStr, 2, Length(ColStr));
// save ColStr to database
end;



It looks like the column order is reversed (so if the grid displays Field1, Field2, Field3... then retrieving it will produce ...Field3, Field2, Field1) and that I have dealt with. Unfortunately it is also reporting an extra field which is not displayed and this is causing problems.

Is there a better way of doing this? I have seen there are procedures for SaveColumnPosition() and saveColumnPos() but I haven't found any useful documentation on them.

Many thanks!

Have you considered using 

grid.ColumnStatesToString
grid.StringToColumnStates
that stores the order, width & visibility of columns in a string and vice versa.
Make sure to:
1) call grid.SetColumnOrder; at the point you have the reference column order & state
2) use grid.StringToColumnStates after your dataset is active, i.e. all columns are in the grid