?
Solved

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

Posted on 2003-03-12
6
Medium Priority
?
321 Views
Last Modified: 2012-05-04
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.
0
Comment
Question by:Arturo Hernandez
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 9

Expert Comment

by:mocarts
ID: 8120583
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
 
LVL 7

Expert Comment

by:Motaz
ID: 8120584
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
 
LVL 1

Author Comment

by:Arturo Hernandez
ID: 8120629
i want to do that without editing the table.. just using the dbgrid..
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Arturo Hernandez
ID: 8120788
i want to do that without editing the table.. just using the dbgrid..
0
 
LVL 9

Accepted Solution

by:
mocarts earned 750 total points
ID: 8121301
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
 
LVL 9

Expert Comment

by:mocarts
ID: 8121342
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question