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