Jump to content

How to delete dbgrid columns?


eduardosuruagy

Recommended Posts

  • 8 months later...

hi ..

 

i use following code (found it here in the forum - did not know the original Poster of the code) 

works perfekt .. the user can select at runtime the position, the size and the visibility of colums ..

 

procedure TyourForm_orFrame.loadGridLayout;
var
  Lines: TStringList;
  columnInfo: TStringList;
  lineCtr: Integer;
  colIdx: Integer;
  cnt: Integer;
  FileName: String;
begin

  FileName := uniservermodule.FilesFolderPath + '/Usersettings/user_' + UniMainModule.UserNumber + '_setting_grid2.txt';

  if FileExists(FileName) then
  begin

    try
      Lines := TStringList.Create;
      columnInfo := TStringList.Create;
      Lines.LoadFromFile(FileName);
      for lineCtr := 0 to Lines.Count - 1 do
      begin
        if trim(Lines[lineCtr]) <> '' then
        begin
          StringExplode(Lines[lineCtr], '~ ', columnInfo);
          cnt := Mydbgrid.Columns.Count;
          // go through all the columns, looking for the one we are currently working on
          for colIdx := 0 to cnt - 1 do
          begin
            // once found, set its width and title, then its index (order)
            if Mydbgrid.Columns[colIdx].FieldName = columnInfo[1] then
            begin
              Mydbgrid.Columns[colIdx].Width := strtoint(columnInfo[3]);
              Mydbgrid.Columns[colIdx].Title.Caption := columnInfo[2];
              if columnInfo[4] = 'true' then
                Mydbgrid.Columns[colIdx].Visible := true
              else
                Mydbgrid.Columns[colIdx].Visible := false;

              // do the index assignment last!
              // ignore the index specified in the file. use its line
              Mydbgrid.Columns[colIdx].Index := lineCtr; // StrToInt(columnInfo[0]); order instead
            end; // if
          end;
        end;
      end;
    finally
      Lines.Free;
      if assigned(columnInfo) then
        columnInfo.Free;
    end;
  end;
end;

procedure TyourForm_orFrame.saveGridLayout;
var
  Lines: TStringList;
  i: Integer;
  my_visible: String;
  FileName: String;
begin

  FileName := uniservermodule.FilesFolderPath + '/Usersettings/user_' + UniMainModule.UserNumber + '_setting_grid2.txt';

  try
    Lines := TStringList.Create;
    with Mydbgrid do
    begin
      for i := 0 to Mydbgrid.Columns.Count - 1 do
      begin
        if Mydbgrid.Columns.Visible = true then
          my_visible := 'true'
        else
          my_visible := 'false';

        Lines.Add(inttostr(Mydbgrid.Columns.Index) + '~ ' + Mydbgrid.Columns.DisplayName + '~ ' + Mydbgrid.Columns.Title.Caption + '~ ' + inttostr(Mydbgrid.Columns.Width) + '~ ' + my_visible);
      end;
    end;

    Lines.SaveToFile(FileName);
  finally
    Lines.Free;
  end;
end;

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...