Jump to content
uniGUI Discussion Forums
Stemon63

DbGrid.ColumnByName

Recommended Posts

Hi Farshad,

please insert a DBgrid.ColumnByname or similar (ByFieldname).

I need to show or hide some columns at runtime, but only Idx is limited and difficult if visible column's number can be dynamic. 

Thanks in advance,

Stefano 

Share this post


Link to post
Share on other sites
procedure TUniMainModule.ColumnVisible(Grid:TUniDBGrid;FieldName:String;visible:boolean);

var i:integer;

begin

   for I := 0 to grid.Columns.Count do

     begin

       if grid.Columns.FieldName=FieldName then

           begin

            grid.Columns.Visible :=Visible;

            break;

           end;

     end;

end;

 

 

use:

 

unimainmodule.ColumnVisible(grid1,'column',true);

Share this post


Link to post
Share on other sites

Hello,

 

Line 4:

 
for I := 0 to grid.Columns.Count-1 do
 
Should be "grid.Columns.Count-1" not "grid.Columns.Count."
 
Please correct me if I am wrong.

Share this post


Link to post
Share on other sites

You can easily extend the TuniDBGrid class as such and create your own component:

TMyUniDBGrid = class(TUniDBGrid)
public
  function ColumnByName(const pColumnName: string): TUniBaseDBGridColumn;
end;

function TMyUniDBGrid.ColumnByName(const pColumnName: string): TUniBaseDBGridColumn;
var i: integer;

begin
  for i := 0 to Columns.Count-1 do
  begin
    if Columns[i].FieldName = pColumnName then
    begin
      Result := Columns[i];
      Break;
    end;
  end;
  if (Result = nil) then
    raise Exception.Create('Column "' + pColumnName + '" does not exist!');
end;

Share this post


Link to post
Share on other sites

Even better:

TUniDBGridHelper = class helper for TUniDBGrid
public
  function ColumnByName(const pColumnName: string): TUniBaseDBGridColumn;
end;

function TUniDBGridHelper.ColumnByName(const pColumnName: string): TUniBaseDBGridColumn;
var i: integer;
begin
  for i := 0 to Columns.Count-1 do
  begin
    if Columns[i].FieldName = pColumnName then
    begin
      Result := Columns[i];
      Break;
    end;
  end;
  if (Result = nil) then
    raise Exception.Create('Column "' + pColumnName + '" does not exist!');
end;
  • Upvote 1

Share this post


Link to post
Share on other sites

×