Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2003-03-12
6
Medium Priority
?
334 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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This is an update to some code that someone else posted on Experts Exchange. It is an alternate approach, I think a little easier to use, & makes sure that things like the Task Bar will update.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

577 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