We help IT Professionals succeed at work.

TDBGrid question

jmiqueli
jmiqueli asked
on
Medium Priority
447 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
Comment
Watch Question

Commented:
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.

Author

Commented:
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

Commented:
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

Author

Commented:
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
jic

Commented:
Be careful with the OnCalcFields event.  It will fire plenty of times.  Make sure that it is a short procedure
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.