If you rewrite your code like this:
var
AField : TField;
begin
AField := DBGrid2.SelectedField;
Caption := AField.ToString;
FDQuery2.Close;
FDQuery2.SQL.Text := 'select column_name from information_schema.columns where table_schema="bases" and table_name = "'+DBGrid2.SelectedField.ToString+'"';
FDQuery2.Open;
ListBox2.Clear;
FDQuery2.GetFieldNames(ListBox2.Items);
and run it, you should be able to see immediately what your problem is.
ToString
does NOT return the value in the selected field of the grid - in your case it returns TStringField
, which is the type of the field. not its string value. To get its string value from the table use DBGRid2.SelectedField.AsString
.
There are a couple of other things I think are wrong with your code:
Using a DBGrid's SelectedField
to access a field's value. In general, that is ok to do, but since in your case you already know which field you need - Table_Name
- what's the point of requiring the user to click it? Also, the user may select the wrong column.
Better to use FDQuery2.FieldByName('Table_Name').AsString
I bet your DBGrid2 is connected to FDQuery2 via its datasource. If so, basing your query on what's already displayed in DBGrid2 is asking for trouble unless you know what you are doing. If you are still working with the code in your other q, you would do better to get the Table_Name value from FDQuery1.
In any case, in my answer to your other q, Display database structure from Delphi (rad studio), I showed you how to set up a master-detail linkage between the two tables so that the second one, the table columns one, is automatically synced with the selected table in the first one.