Solved

TDBGrid question

Posted on 1997-03-11
6
409 Views
Last Modified: 2010-04-04
I'm using a TDBGrid in one of my dialogs.  I've set the DataSource and modified the columns in the Column Editor.  My problem is that there is one column that is not part of the database.  I need to dynamically modify the column myself. In other words, I've added a column in the Column Editor and left FieldName blank.  Is there anyway I can get access to the column in the TDBGrid and add the data I need?

Thanks,
Juan C. Miqueli
juan.miqueli@turner.com
0
Comment
Question by:jmiqueli
6 Comments
 

Expert Comment

by:pscadden
ID: 1334737
Double-click the dataset component associated with the
datasource to bring up the Fields editor. Go to New and
create a calculated field. This field with the name you give it will
now appear in the TDbgrid. You can use the event for
calculated field to get the information. OR - you can simply
make leave that blank (which I think is what you want) and
the user can enter the field and enter value appropriate
to the other details of the field.
0
 

Author Comment

by:jmiqueli
ID: 1334738
I added the new field and made it a calculated field.  The name came up in the Column Editor.  My only question is, where is the event for calculated fields?  Is it the OnGetData?

Juan
0
 

Expert Comment

by:Singeon
ID: 1334739
The event for calculated fields will be in the Event list for your actual TQuery or TTable dataset component.  Click on the dataset component,  go to its event list, and you will see "OnCalcFields", within which you can set the value of the field.  For example, a field named FIELDNAME in a query component named MyQuery can be set like this:

MyQueryFIELDNAME.Value := ...


Dale

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:jmiqueli
ID: 1334740
On the dataset component associated with the datasource, after I entered the new field and made it a Calc field. I went to the event on that field. In the OnGetText of the field there is var parameter passed in called Text. I set the Text field to whatever I needed and that worked. I'm going to try your example.

Thanks for your help,
Juan
0
 

Expert Comment

by:jic
ID: 1334741
Be careful with the OnCalcFields event.  It will fire plenty of times.  Make sure that it is a short procedure
0
 

Accepted Solution

by:
jameshui earned 100 total points
ID: 1334742
Hi!
I want to make things clear first. Are you want to dynamically
modify a column and can rename the field name, right? You want to modify them in the design time or run time?

If you want to modify it on design time, the comment from other user have given a very clear procedure, and I think you can handle it.

If you want to modify it on run time, the main problem is cause by renaming the field's name. Because it will destory the structure of the original database file, so DELPHI will not let user to do so.

Therefore the solution is simple, the only possible way is to create a new table for the database file, so that you can constructe a new structure for it. After created the new table and just copy all the content of the old one.

Creating a new table is quite simple.
(Please take a look on the help file in DELPHI)
(The following code is copied from the DELPHI Help File 'Createtable' item.)

with Table1 do
begin
  Active := False;
  DatabaseName := 'Delphi_Demos';
  TableName := 'CustInfo';
  TableType := ttParadox;
  with FieldDefs do
    begin
    Clear;
    Add('Field1', ftInteger, 0);
    Add('Field2', ftInteger, 0);
    end;
  with IndexDefs do
    begin
    Clear;
    Add('Field1Index', 'Field1', [ixPrimary, ixUnique]);
    end;
CreateTable;
end;

But the creation of a new database is quite slow, if you modify the database's field name frequently, it would become very time wasting.

In order to save time, I suggest that you can use TGrid(Text Grid). You can copy all the data from the database file to TGrid and let user modify them, and you just need to create the new table when user want to save their data.

The TGrid is more easy to handle by programmer if he do need to change the content in the grid oftenly, but the disadvantage is it just can keep the data but can not give the function as database (e.g. SQL, Linking data...).

Help it helps.

James Hui
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now