[SOLVED] Obtain the DataType of selected grid column? lazarus / 16-Databases ---------------------------------------------------------------------- Elmug · 2012-06-29 06:49 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! ---------------------------------------------------------------------- Lacak2 · 2012-06-29 08:03 Quote from: Elmug on June 29, 2012, 08:49:35 amSince we have DBGrid1.SelectedColumn.FieldName and I have used it alredy, is there perhaps a similar equivalent, perhaps like DBGrid1.SelectedColumn.DataType? Exactly ---------------------------------------------------------------------- BigChimp · 2012-06-29 08:27 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... ---------------------------------------------------------------------- Elmug · 2012-06-29 08:34 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 · 2012-06-29 08:42 Yep - not being able to find these things out yourselves can be incredibly annoying... ---------------------------------------------------------------------- Elmug · 2012-06-29 10:00 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 · 2012-06-29 13:19 And what about Code: [Select]DBGrid1.SelectedField.DataType;  Documentation is here: http://lazarus- ccr.sourceforge.net/docs/lcl/dbgrids/tcustomdbgrid.html ---------------------------------------------------------------------- Elmug · 2012-06-29 19:03 Quote from: ludob on June 29, 2012, 03:19:59 pmAnd 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 · 2012-06-30 07:37 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! ---------------------------------------------------------------------- ludob · 2012-06-30 08:10 QuoteI got it working now, as follows:Good  Shorter: Code: [Select]E_FieldDataType.Text:=Fieldtypenames[dbgrid1.SelectedFie ld.DataType]; Documentation: http://www.freepascal.org/docs- html/fcl/db/fieldtypenames.html ---------------------------------------------------------------------- Elmug · 2012-06-30 09:06 Quote from: ludob on June 30, 2012, 10:10:37 amQuoteI got it working now, as follows:Good  Shorter: Code: [Select]E_FieldDataType.Text:=Fieldtypenames[dbgrid1.SelectedFie ld.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 · 2012-06-30 09:55 QuoteIt 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 · 2012-06-30 12:41 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 · 2012-06-30 17:09 Quote from: ludob on June 30, 2012, 11:55:05 amQuoteIt 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. ---------------------------------------------------------------------- Elmug · 2012-06-30 17:16 Quote from: KpjComp on June 30, 2012, 02:41:08 pmThere 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. ---------------------------------------------------------------------- KpjComp · 2012-06-30 18:48 QuoteI think the array is predefined by the Unit DB Yes, that's why I said "Say if Fieldtypenames array had not been defined,". I only pointed out for future reference, where you might want to get the Names of other Enums. ---------------------------------------------------------------------- Elmug · 2012-06-30 21:59 Quote from: KpjComp on June 30, 2012, 08:48:23 pmQuoteI think the array is predefined by the Unit DB Yes, that's why I said "Say if Fieldtypenames array had not been defined,". I only pointed out for future reference, where you might want to get the Names of other Enums. I do find your tip very helpful and useful and as guide to many other possibilities, and for my further studying and understanding. Thank you.