2
votes

I have a delphi form with TFDConnection, TFDQuery, TDataSource and TDBGrid. I want to show the data that my query gets (select * from table_1) and put it in my DBGrid. I've made all the connections between the components, but after I launch my query (which returns 12 rows) in my DBGrid are shown 12 rows but they are empty. Apparentely I forgot something that prevents me from seeing my data in my DBGrid. Can you help me what I'm missing in my actions ?

  • 1- Adding all components to the form.
  • 2- Connect FDQuery with FDConnection, TDataSource with FDQuery, DBGrid with TDataSource.
  • 3- Connection parameters succed in testing (the problem is not in my connection parameters)
  • 4- The number of rows are placed in my DBGrid (12), but with no information visible.
  • 5- If I loop through my FDQuery I can see all the information I need but that doesn't help me populate my DBGrid.
5
It should work. I just repeated all the steps: A TFDConnection, created connection to SQLite database, tested connection, even ran a test query in the connection dialog SQL tab. Dropped a TFDQuery, set the connection to FDConnection1 and SQL to the test query used before. Set FDConnection1.Connected to True, FDQuery1.Active to True, dropped a TDataSource, set the DataSet to FDQuery1. Dropped a TDBGri and set DBGrid1.DataSource to DataSource1, and the data appeared in the DBGrid. - Ken White
Are the fields defined to be visible? I suppose you could set everything up then define the fields to be invisible. - No'am Newman
And what happens if you explicitly give up the needed fields in your select statement instead of using 'select *' ? - Copilot
@KenWhite -> The thing is I have to fill my connection parameters run-time not design-time and that's why I can't test it design-time. - Viktor Anastasov
@No'amNewman -> If I undersand correctly you mean that somewhere something happens that makes my fields invisible - after testing it showed me that all my fields are visible so this is not where my problem is coming. - Viktor Anastasov

5 Answers

3
votes

Can you try this:

Make your TFDQuery active explicitly in your OnShow code of your Form. Like this,

procedure TForm.FormShow(Sender: TObject);
begin
    FDQuery.Active:= True;
end;

It works for me and hope it helps.

1
votes

You would get the behaviour that you would see the correct number of rows in the DBGrid, but the grid's cells are empty, if the DBGrid's DefaultDrawing property is set to False. if that's the case for your DBGrid, obviously just set DefaultDrawing to True.

1
votes

To fix this problem (which I had the same), after you open the query, set:

datasource.dataset := Query

and the problem will be resolved.

0
votes

I know the topic is old, but maybe it can help someone. In my case, the solution was: Deleting all the DBGrid columns that was created in design time, OR match the column's (TColumn) FieldName property to the EXACT field names as they named in the database tables.

-1
votes

For anyone else with this problem, here's the fix: make sure that the column names (not the Title) in the grid control match the field names in the query. Once they're matched they should behave properly.