Modify the contents of a DBGrid cell before displaying it

I want to change the contents of a specific cell in a dbgrid control when loading the database. For example, let's say I do not want any database field to appear in dbgrid if it is "forbidden" . Is there any way I can do this?

+3
source share
6 answers

you can use DataSetNotifyEvent Afteropen

DBGrid.Datasource.Dataset.Afteropen: =

and you can hide the fields with:

if the condition DBGrid.columns [x] .visible: = false

you can check the condition for the OnDrawColumnCell event to override / delete some content in a specific cell

+3
source

:

OnGetText , , .

DisplayText , False, .

procedure TForm1.SQLQuery1Field1GetText(Sender: TField; 
  var Text: string; DisplayText: Boolean);
begin
  if (Sender.AsString = 'forbidden') and (DisplayText) 
    and (PrivilegeLevel(CurrentUser) < 10) then
    Text := '********'
  else
    Text := Sender.AsString;
end;
+5

DataSet . DataSource , -.

, DataSet dsInactive dsBrowse, DataSource OnState, - UI, .

Auxiliar , , .

( )

procedure TForm1.DataSource1StateChange(Sender: TObject);
begin
  if (DataSource1.State = dsBrowse) and (not FUIStateInSync) then
  begin
    //dataset is open, change UI accordingly
    DBGrid1.Columns[0].Visible := SomeCondition();
    //this will prevent the code to be executed again 
    //as state comes to dsBrowse after posting changes, etc. 
    FUIStateInSync := True; 
  end
  else if (DataSource1.State = dsInactive) then
    FUIStateInSync := False; //to let it happen again when opened.
end;

, , O.D. - , .

+3

Hookup OnAfterOpen event in the dataset. Get the hidden fields and set the Visible property to False and your dbgrid will not display them.

Greetings

+2
source

I would modify the query that provides the data in the grid so as not to include rows (tuples) that have a β€œforbidden” row. This is much easier than trying not to display the data after it has already been extracted from the database.

+2
source

I think the best way would not be SELECT fields WHERE SOME_VALUE = "forbidden" FROM the DATABASE_TABLE

+1
source

Source: https://habr.com/ru/post/1790075/


All Articles