Jump to content

individual Columns in uniDBGrid


erich.wanker

Recommended Posts

Hello folks ;-)

Question:

How can i give a user following solution:

Select fieldnames, what should shown in uniDBGrid at runtime...

position change of colums at runtime

save this settings - and Load this settings

.. has someone a code sniple ???

ThanX

Erich

  • Like 2
Link to comment
Share on other sites

ok ... got it :-)

Found something simular in a lazarus forum ... and it works perfect :-)

 

 


//
//
// Original Code found at: http://forum.lazarus.freepascal.org/index.php?topic=21705.0
// nice greetings .. erich


procedure TV_FRAME_VERTRAG_STRUKTUR.StringExplode(s: string; Delimiter: string; Var res: TStringList);
Begin
    res.Clear;
    res.Text := StringReplace(s, Delimiter, #13#10, [rfIgnoreCase, rfReplaceAll]);
End;

procedure TV_FRAME_VERTRAG_STRUKTUR.saveGridLayout(Mydbgrid: TuniDBGrid; fileName: string);
var
  lines: TStringList;
  i: integer;
  my_visible:String;
begin
    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;


procedure TV_FRAME_VERTRAG_STRUKTUR.loadGridLayout(Mydbgrid: TuniDBGrid; fileName: string);
var
    lines: TStringList;
    columnInfo: TStringList;
    lineCtr: integer;
       colIdx: integer;
        cnt: integer;
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;

 

 

 

Link to comment
Share on other sites

i have tested it with just 10 - 15 columns ... and it works fine for me.. 

(before every menupoint change - i save the actual settings and load the settings of the new menu-point ..  and i have no performance  problems...)

 

 

Link to comment
Share on other sites

  • 2 years later...

Here is the code above a bit modified and without the bugs (this time for the mobile version):

PROCEDURE TUniMainModule.StringExplode(s : STRING; Delimiter : STRING; VAR res : TStringList);
BEGIN
    res.Clear;
    res.Text:=StringReplace(s, Delimiter, #13#10, [rfIgnoreCase, rfReplaceAll]);
END;

PROCEDURE TUniMainModule.SaveGridLayout(Mydbgrid : TUnimDBGrid);
VAR filename : STRING;
    lines : TStringList;
    i : INTEGER;
    my_visible : STRING;
BEGIN
    Filename:=UniServerModule.StartPath+'GRID'+IntToStr(Mydbgrid.Tag)+'.INI';
    TRY
        lines:=TStringList.Create;
        WITH Mydbgrid DO BEGIN
            FOR i:=0 TO Mydbgrid.Columns.count-1 DO BEGIN
                IF Mydbgrid.Columns[i].Visible=TRUE THEN my_visible:='T' 
      			ELSE my_visible:='F';
 			    lines.Add(
                  Mydbgrid.Columns[i].DisplayName+';;'
                  +IntToStr(Mydbgrid.Columns[i].Width)+';;'
                  +Mydbgrid.Columns[i].Title.Caption+';;'
                  +my_visible);
            END;
        END;
        lines.SaveToFile(fileName);
    FINALLY
        lines.free;
    END;
END;

PROCEDURE TUniMainModule.LoadGridLayout(Mydbgrid : TUnimDBGrid);
VAR filename : STRING;
    lines : TStringList;
    columnInfo : TStringList;
    lineCtr : INTEGER;
    colIdx : INTEGER;
    cnt : INTEGER;
BEGIN
    Filename:=UniServerModule.StartPath+'GRID'+IntToStr(Mydbgrid.Tag)+'.INI';
    IF NOT TFile.Exists(Filename) THEN Exit;
    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[0] THEN BEGIN
                        Mydbgrid.Columns[colIdx].Width:=StrToInt(columnInfo[1]);
                        Mydbgrid.Columns[colIdx].Title.Caption:=columnInfo[2];
                        IF columnInfo[3]='T' THEN Mydbgrid.Columns[colIdx].Visible:=TRUE ELSE Mydbgrid.Columns[colIdx].Visible:=false;
                        Mydbgrid.Columns[colIdx].Index:=lineCtr;
                    END;
                END;
            END;
        END;
    FINALLY
        lines.free;
        IF assigned(columnInfo) THEN columnInfo.free;
    END;
END;

To make it work, put this code into your MainModule, add "unimDBGrid" and "System.IOUtils" into the uses segment and give every grid in your app a different Tag-property. 

Use LoadGridLayout / SaveGridLayout with the corresponding grid. It's recommend to load all grids in the FormShow-event and save them in the FormCloseEvent.

For desktop-version change "TUnimDBGrid" to "TUniDBGrid".

  • Like 1
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...