Stemon63 Posted April 18, 2017 Share Posted April 18, 2017 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 Link to comment Share on other sites More sharing options...
Wilton Ergon Posted September 3, 2017 Share Posted September 3, 2017 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); 1 Link to comment Share on other sites More sharing options...
sbokhari Posted September 3, 2017 Share Posted September 3, 2017 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. Link to comment Share on other sites More sharing options...
Stemon63 Posted September 5, 2017 Author Share Posted September 5, 2017 Thanks!But a native implementation is better... :-) Good Job! Stefano Link to comment Share on other sites More sharing options...
GerhardV Posted September 11, 2017 Share Posted September 11, 2017 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; Link to comment Share on other sites More sharing options...
rgreat Posted September 11, 2017 Share Posted September 11, 2017 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 Link to comment Share on other sites More sharing options...
GerhardV Posted September 12, 2017 Share Posted September 12, 2017 Yep good point...forgot about the helper (decorator) classes Link to comment Share on other sites More sharing options...
sbokhari Posted September 13, 2017 Share Posted September 13, 2017 Hello GerhardV, if Columns[i].FieldName = pColumnName then The comparision should be case insensitive. Regards Link to comment Share on other sites More sharing options...
GerhardV Posted September 14, 2017 Share Posted September 14, 2017 Thank you. if SameText(Columns[i].FieldName, pColumnName) then ... Link to comment Share on other sites More sharing options...
Recommended Posts