Populating a TDbGrid lazarus / 16-Databases ---------------------------------------------------------------------- hac3ru · 2014-02-23 12:14 Hello, I have the following code: Code: [Select]procedure TForm1.Button2Click(Sender: TObject); var count : integer;     i     : integer; begin   count := 0;   Form2 := TForm2.Create(self);   Form2.Datasource1.DataSet := SQLQuery1;   Form2.DBGrid1.DataSource := Form2.Datasource1;    SQLQuery1.SQL.Text:= 'SHOW columns FROM testtable';   SQLQuery1.Open;   While not SQLQuery1.EOF do   begin     Form2.DBGrid1.Columns.Add;     Form2.DBGrid1.Columns.Items[count].Title.Caption := SQLQuery1.Fields[0].AsString;     Inc(count);     SQLQuery1.Next;   end;   SQLQuery1.Close;   SQLQuery1.SQL.Text := 'SELECT * FROM testtable';   Form2.Datasource1.Enabled := True;   SQLQuery1.Open;    Form2.ShowModal;   Form2.Free;   SQLQuery1.Close; The problem is that the DbGrid is not written correctly. I see the number of entries (if the Db has 7 entries I get 7 empty fields in the Grid). I checked to see if the datasource is correctly populated with Code: [Select]While not SQLQuery1.EOF do   begin     Memo1.Lines.Add(Form2.Datasource1.DataSet.Fields[0].AsString);     Memo1.Lines.Add(Form2.Datasource1.DataSet.Fields[1].AsString);     Memo1.Lines.Add(Form2.Datasource1.DataSet.Fields[2].AsString);     SQLQuery1.Next;   end;        This returned the values from the datasource which seem to be the ones in the Database so the Datasource is written correctly.... Anyone any ideas? ---------------------------------------------------------------------- taazz · 2014-02-23 13:55 1) dbgrid does not require you to add the columns manually, simple opening a linked dataset should auto populate the columns collection with all the columns returned by the query using the field's name as column title. 2) you do not link the columns to the required fields eg Code: [Select]DBGrid1.Columns.Items[0].FieldName := SQLQuery1.Fields[0].AsString. ---------------------------------------------------------------------- hac3ru · 2014-02-23 17:20 Quote from: taazz on February 23, 2014, 02:55:23 pm1) dbgrid does not require you to add the columns manually, simple opening a linked dataset should auto populate the columns collection with all the columns returned by the query using the field's name as column title. 2) you do not link the columns to the required fields eg Code: [Select]DBGrid1.Columns.Items[0].FieldName := SQLQuery1.Fields[0].AsString. That's what I did first and it didn't work so I manually added the columns. Data in teh Dataset is correct. The DBGrid is not showing it though. ---------------------------------------------------------------------- wp · 2014-02-23 17:42 Fields[0].AsString? That's the field value of the current record. I am pretty sure that you should use "Fields[0].FieldName" instead. ---------------------------------------------------------------------- taazz · 2014-02-23 17:50 I assume that the command "SHOW columns FROM testtable" will return the fields of  testTable as values in the rows having multiple columns based on the field information eg fields[0] is the field name fields[1] is the datatype etc. You have to make sure that the fieldname gets the value of the field's name not data type or anything else that might get returned by the sql command. As for the details of what is returned I will leave it to people that use MYSQL to answer it personally I stay away from MySQL at all cost. ---------------------------------------------------------------------- wp · 2014-02-23 19:10 QuoteI assume that the command "SHOW columns FROM testtable" will return the fields of  testTable as values in the rows Ah - I must confess that I did not read the first posting... ---------------------------------------------------------------------- mangakissa · 2014-02-24 10:30 look at option 1 from taazz. DBGrid only works with a datalink to a dataset. And why using the datacomponent again if you want to see the first dataset. if this code Code: [Select]count := 0;   Form2 := TForm2.Create(self);   Form2.Datasource1.DataSet := SQLQuery1;   Form2.DBGrid1.DataSource := Form2.Datasource1;    SQLQuery1.SQL.Text:= 'SHOW columns FROM testtable';   SQLQuery1.Open; //  SQLQuery1.Close; //  SQLQuery1.SQL.Text := 'SELECT * FROM testtable'; //  Form2.Datasource1.Enabled := True; //  SQLQuery1.Open;    Form2.ShowModal;   Form2.Free;   SQLQuery1.Close; ---------------------------------------------------------------------- taazz · 2014-02-24 16:37 Quote from: hac3ru on February 23, 2014, 06:20:03 pmQuote from: taazz on February 23, 2014, 02:55:23 pm1) dbgrid does not require you to add the columns manually, simple opening a linked dataset should auto populate the columns collection with all the columns returned by the query using the field's name as column title. 2) you do not link the columns to the required fields eg Code: [Select]DBGrid1.Columns.Items[0].FieldName := SQLQuery1.Fields[0].AsString. That's what I did first and it didn't work so I manually added the columns. Data in teh Dataset is correct. The DBGrid is not showing it though. What did you do first? allow the dbgrid to auto populate its columns? For me it does work and it is easy to check. Add all components to a form (connection, dataset, datasource, dbgrid etc) link them togethere add the "select * from testTable" on the query-dataset and then open the connection and activate the dataset does the dbgrid show the columns? If not please create a basic sample witch shows the problem and I"ll take a closer look.