Recent

Author Topic: [SOLVED] Obtain the DataType of selected grid column?  (Read 11119 times)

Elmug

  • Hero Member
  • *****
  • Posts: 849
[SOLVED] Obtain the DataType of selected grid column?
« on: June 29, 2012, 08:49:35 am »
Hi everyone,

SQLite3, Windows7, SQLite3Connection:

I would like to detect the datatype of the selected or current grid column.

Since we have DBGrid1.SelectedColumn.FieldName and I have used it alredy, is there perhaps a similar equivalent, perhaps like DBGrid1.SelectedColumn.DataType?

If so, I would appreciate info on it, or a way to obtain the DataType of the current column in the Grid.

Thanks a lot!
« Last Edit: June 30, 2012, 09:45:44 am by Elmug »

Lacak2

  • Guest
Re: Obtain the DataType of selected grid column?
« Reply #1 on: June 29, 2012, 10:03:57 am »
Since we have DBGrid1.SelectedColumn.FieldName and I have used it alredy, is there perhaps a similar equivalent, perhaps like DBGrid1.SelectedColumn.DataType?
Exactly  :D

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Obtain the DataType of selected grid column?
« Reply #2 on: June 29, 2012, 10:27:26 am »
Long live autocompletion (type a period after your object, then ctrl-space - it shows you all possible properties and methods/functions of an object)... or F1 or right click, show declaration...
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Elmug

  • Hero Member
  • *****
  • Posts: 849
Re: Obtain the DataType of selected grid column?
« Reply #3 on: June 29, 2012, 10:34:01 am »
Thank you a lot, Lacak2 and BigChimp.

I think this will solve what I needed in relation to doing some dynamics to tables with Memos.

With what you are expaining now, BigChimp, I will at least now be on a bycicle, rather than on foot. I really needed to know that.

Best of all to all.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Obtain the DataType of selected grid column?
« Reply #4 on: June 29, 2012, 10:42:42 am »
Yep - not being able to find these things out yourselves can be incredibly annoying...
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Elmug

  • Hero Member
  • *****
  • Posts: 849
Re: Obtain the DataType of selected grid column?
« Reply #5 on: June 29, 2012, 12:00:05 pm »
Hi, fellows, again:

Unfortunately, it seems that DataType is not available as wished.

I added this line:
Edit1.Text := DBGrid1.SelectedColumn.DataType; 
and the error message is:
identifier idents no menber "DataType"


Also, the autocomplete does show the member FieldName, but does not show "DataType" as available :'(.

I would appreciate some other suggestions.

Thanks again!

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: Obtain the DataType of selected grid column?
« Reply #6 on: June 29, 2012, 03:19:59 pm »
And what about
Code: [Select]
DBGrid1.SelectedField.DataType; 
Documentation is here: http://lazarus-ccr.sourceforge.net/docs/lcl/dbgrids/tcustomdbgrid.html

Elmug

  • Hero Member
  • *****
  • Posts: 849
Re: Obtain the DataType of selected grid column?
« Reply #7 on: June 29, 2012, 09:03:11 pm »
And what about
Code: [Select]
DBGrid1.SelectedField.DataType; 
Documentation is here: http://lazarus-ccr.sourceforge.net/docs/lcl/dbgrids/tcustomdbgrid.html

Hi Ludob, much appreciation for the tip.

SelectedField.DataType is available, and likely can solve what I am trying to do.

I am off to studying how to deal with TranslateString types, for this, and will report back.

Thanks!

Elmug

  • Hero Member
  • *****
  • Posts: 849
Re: Obtain the DataType of selected grid column?
« Reply #8 on: June 30, 2012, 09:37:11 am »
Hi everyone,

I got it working now, as follows:

// This procedure puts the datatype of TDBGrid's current cell into a TEdit box
PROCEDURE TForm1.DBGrid1ColEnter(Sender: TObject);
VAR X: TFieldType;
BEGIN
   X:= DBGrid1.SelectedField.DataType; //X here obtains datatype of Grid's selected cell
   //So, use FldToStr function, placed at implementation section,
   //to get datatype in text-readable form, and put it into a TEdit box
     E_FieldDataType.Text :=  FldToStr(X); {E_FieldDataType is the TEdit box}
END;             
//Note: Function FldToStr (USES Unit Db) was used exactly as found from: 


http://www.codepedia.com/1/FieldToStr

and am including it below, for convenience:
Code: [Select]
function FldToStr: String;  //(const Value : TFieldType) : String;
begin
   Case Value of
   ftUnknown    :  FldToStr := 'Unknown';
   ftString :  FldToStr := 'String';
   ftSmallint :  FldToStr := 'SmallInt';
   ftInteger :  FldToStr := 'Integer';
   ftWord :  FldToStr := 'Word';
   ftBoolean :  FldToStr := 'Boolean';
   ftFloat :  FldToStr := 'Float';
   ftCurrency :  FldToStr := 'Currency';
   ftBCD :  FldToStr := 'BCD';
   ftDate :  FldToStr := 'Date';
   ftTime :  FldToStr := 'Time';
   ftDateTime :  FldToStr := 'DateTime';
   ftBytes :  FldToStr := 'Bytes';
   ftVarBytes   :  FldToStr := 'VarBytes';
   ftAutoInc :  FldToStr := 'AutoInc';
   ftBlob :  FldToStr := 'Blob';
   ftMemo :  FldToStr := 'Memo';
   ftGraphic :  FldToStr := 'Graphic';
   ftFmtMemo :  FldToStr := 'FmtMemo';
   ftParadoxOle :  FldToStr := 'ParadoxOle';
   ftDBaseOle :  FldToStr := 'DBaseOle';
   ftTypedBinary: FldToStr := 'TypedBinary';
   ftCursor     :  FldToStr := 'Cursor';
   ftFixedChar :  FldToStr := 'FixedChar';
   ftWideString :  FldToStr := 'WideString';
   ftLargeint :  FldToStr := 'LargeInt';
   ftADT :  FldToStr := 'ADT';
   ftArray :  FldToStr := 'Array';
   ftReference :  FldToStr := 'Reference';
   ftDataSet :  FldToStr := 'DataSet';
   ftOraBlob :  FldToStr := 'OraBlob';
   ftOraClob :  FldToStr := 'OraClob';
   ftVariant :  FldToStr := 'Variant';
   ftInterface :  FldToStr := 'Interface';
   ftIDispatch :  FldToStr := 'IDispatch';
   ftGuid :  FldToStr := 'GUID';
   ftTimeStamp :  FldToStr := 'TimeStamp';
   ftFMTBcd :  FldToStr := 'FMTBcd';
   end;
end;

Thank you all for helping me resolve this! :)
« Last Edit: June 30, 2012, 09:44:11 am by Elmug »

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: [SOLVED] Obtain the DataType of selected grid column?
« Reply #9 on: June 30, 2012, 10:10:37 am »
Quote
I got it working now, as follows:
Good  ;)

Shorter:
Code: [Select]
E_FieldDataType.Text:=Fieldtypenames[dbgrid1.SelectedField.DataType];
Documentation: http://www.freepascal.org/docs-html/fcl/db/fieldtypenames.html

Elmug

  • Hero Member
  • *****
  • Posts: 849
Re: [SOLVED] Obtain the DataType of selected grid column?
« Reply #10 on: June 30, 2012, 11:06:22 am »
Quote
I got it working now, as follows:
Good  ;)

Shorter:
Code: [Select]
E_FieldDataType.Text:=Fieldtypenames[dbgrid1.SelectedField.DataType];
Documentation: http://www.freepascal.org/docs-html/fcl/db/fieldtypenames.html

BEAUTIFUL!

I just tested your MUCH shorter way and worked the first time and will use it instead.
Its like from bicycle to airplane! ::) (It took me 2 days the long way).

Thanks!



ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: [SOLVED] Obtain the DataType of selected grid column?
« Reply #11 on: June 30, 2012, 11:55:05 am »
Quote
It took me 2 days the long way.
It took me 5 minutes to look it up in the documentation. Perhaps you should start there ;)

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: [SOLVED] Obtain the DataType of selected grid column?
« Reply #12 on: June 30, 2012, 02:41:08 pm »
There is another trick you can use too.

Say if Fieldtypenames array had not been defined, you could use type info.

eg.
Code: [Select]
function FieldTypeToString(f:TFieldType):string;
begin
  result := copy( getenumname(typeinfo(TFieldType),ord(f)), 3, 100);
end;

Elmug

  • Hero Member
  • *****
  • Posts: 849
Re: [SOLVED] Obtain the DataType of selected grid column?
« Reply #13 on: June 30, 2012, 07:09:26 pm »
Quote
It took me 2 days the long way.
It took me 5 minutes to look it up in the documentation. Perhaps you should start there ;)

Unfortunately, I always have to start from where I am (most of the two days was READING documentation and all the web would show).

Now I am a little further, though, and I appreciate the help.
« Last Edit: June 30, 2012, 07:47:34 pm by Elmug »

Elmug

  • Hero Member
  • *****
  • Posts: 849
Re: [SOLVED] Obtain the DataType of selected grid column?
« Reply #14 on: June 30, 2012, 07:16:41 pm »
There is another trick you can use too.

Say if Fieldtypenames array had not been defined, you could use type info.

eg.
Code: [Select]
function FieldTypeToString(f:TFieldType):string;
begin
  result := copy( getenumname(typeinfo(TFieldType),ord(f)), 3, 100);
end;

Thanks KpjComp.

Good tip.....I think the array is predefined by the Unit DB, so the doing just takes the one-line ludob gives, with no need to define a function. I had no need to define any array.

 

TinyPortal © 2005-2018