Jump to content


Photo

DbGrid.ColumnByName


  • Please log in to reply
8 replies to this topic

#1 Stefano Monterisi

Stefano Monterisi

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 103 posts

Posted 18 April 2017 - 10:37 AM

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 


  • 0

#2 wilton_rad

wilton_rad

    Active Member

  • uniGUI Subscriber
  • PipPipPip
  • 62 posts

Posted 03 September 2017 - 04:25 AM

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[i].FieldName=FieldName then
           begin
            grid.Columns[i].Visible :=Visible;
            break;
           end;
     end;
end;
 
 
use:
 
unimainmodule.ColumnVisible(grid1,'column',true);

  • 0

#3 SBokhari

SBokhari

    Member

  • Members
  • PipPip
  • 14 posts

Posted 03 September 2017 - 11:43 PM

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.

  • 0

#4 Stefano Monterisi

Stefano Monterisi

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 103 posts

Posted 05 September 2017 - 02:46 PM

Thanks!
But a native implementation is better... :-) 

 

Good Job!

 

Stefano


  • 0

#5 GerhardV

GerhardV

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 134 posts
  • LocationSouth Africa

Posted 11 September 2017 - 07:13 AM

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;

  • 0

uniGUI - LOVE IT!


#6 rgreat

rgreat

    Active Member

  • uniGUI Subscriber
  • PipPipPip
  • 91 posts

Posted 11 September 2017 - 06:34 PM

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;

  • 1

#7 GerhardV

GerhardV

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 134 posts
  • LocationSouth Africa

Posted 12 September 2017 - 07:40 AM

Yep good point...forgot about the helper (decorator) classes :)


  • 0

uniGUI - LOVE IT!


#8 SBokhari

SBokhari

    Member

  • Members
  • PipPip
  • 14 posts

Posted 13 September 2017 - 11:59 PM

Hello GerhardV,

 
if Columns[i].FieldName = pColumnName then

The comparision should be case insensitive.

 

Regards


  • 0

#9 GerhardV

GerhardV

    Advanced Member

  • uniGUI Subscriber
  • PipPipPipPip
  • 134 posts
  • LocationSouth Africa

Posted 14 September 2017 - 12:44 PM

Thank you.

if SameText(Columns[i].FieldName, pColumnName) then
...

  • 0

uniGUI - LOVE IT!





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users