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.
LVL 1
Arturo HernandezAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mocartsCommented:
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.
0
MotazCommented:
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
0
Arturo HernandezAuthor Commented:
i want to do that without editing the table.. just using the dbgrid..
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Arturo HernandezAuthor Commented:
i want to do that without editing the table.. just using the dbgrid..
0
mocartsCommented:
another way is to use Grids OnDrawColumnCell:

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  r: TRect;
begin
  if Column.Title.Caption = 'MyColumnCaption' then
  begin
    r.left := Rect.Left;
    r.Top := Rect.Top;
    r.Bottom := Rect.Bottom;
    r.Right := Rect.Right;
    InflateRect(r, -2, -1);
    if gdSelected in State then
    begin
      DBGrid1.Canvas.Brush.Color := clHighlight;
      DBGrid1.Canvas.Pen.Color := clHighlightText;
      DBGrid1.Canvas.FillRect(Rect);
    end;
    DBGrid1.Canvas.TextRect(r, r.Left, r.Top, 'MyText');
  end;
end;

wbr, mo.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mocartsCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.