Link to home
Start Free TrialLog in
Avatar of Arturo Hernandez
Arturo HernandezFlag for Mexico

asked on

Displaying a String in a Column with a FieldName Null in aTDBGrid

I Have a TDbgrig linked to a table, that dbgrid has 5 columns but one of them is  fieldname null, and i want to display a Text in that field depending on another field's data.

Is that posible? and How i do it?

Thanks.
Avatar of mocarts
mocarts

one way (prefferable) - add to your table Calculated field of apropriate type and assign OnCalcFields event of TTable where set your fields value depending on other field values.

procedure TForm1.Table1CalcFields(DataSet: TDataSet);
begin
  if DataSet.FieldByName('Field1').AsInteger > 100 then
    DataSet.FieldByName('CalcField1').AsString := 'You are rich!'
  else
    DataSet.FieldByName('CalcField1').AsString := 'Get back to work!';
end;

in this case you must add all fields in FieldDefs collection.

wbr, mo.
You need a calulated field, that null field name shouldn't be exist in physical file name, instead you can create it on the fly, example:

------------------------
One of the most good features of Delphi database programming is calculated fields. By using calculated fields you can create new fields that does not exist in original database tables. These fields can be displayed as any normal read-only fields. Example:

Suppose that you have two fields in your table: A, and B. Both fields are Integers. Example of data:

  A  |  B
  --------
  2  |  3
  1  |  2
  7  |  3
  4  |  4

You need to display the summation of the two fields of one record in a new calculated field to be displayed as:

  A  |  B  |  C
  --------------
  2  |  3  |  5
  1  |  2  |  3
  7  |  3  |  10
  4  |  4  |  8

To achive this follow below steps:

- Drop a table and link it with your table that contains A, and B fields.
- Double-click at Table1
- At fields editor right click and select Add all fields
- Right click again and select New Field
- At name box write C. At Type combo box select Integer. At field type select Calculated.
- Return to Table1 events and write below statement at OnCalcFields:

  DataSet.FieldByName('C').AsInteger:=
    DataSet.FieldByName('A').AsInteger + DataSet.FieldByName('B').AsInteger;

- Link your table with any data control such dbGrid and see the new calculated field.


Notes:

- Calculated fields can be used with any data sets including: BDE components, ADO components, dbExpress, Interbase components. Example: TTable, TQuery, TADOTable, TADOQuery, TIBTable, TIBQuery, etc.

- Calculated fields can be used with any data types

--------------------
From My EBook: Delphi programming guide
Motaz
Avatar of Arturo Hernandez

ASKER

i want to do that without editing the table.. just using the dbgrid..
i want to do that without editing the table.. just using the dbgrid..
ASKER CERTIFIED SOLUTION
Avatar of mocarts
mocarts

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
your displayed DataSet will be on record wich is drawn.

so, you can use
  with DBGrid1.DataSource.DataSet do
    if FieldByName('Field1').AsFloat > 100 then
      MyDisplayText := '...'
   
  etc.
mo.