In this question my objective is to retrieve a database table content. populate dbGrid, close connection. If I use the following code, dbgrid or combobox are going to loose the information.
adoQry := TADOQuery.Create(self); adoQry.Connection := adoConn; adoQry.SQL.Add(SqlStr); adoQry.Prepared := true; try adoQry.Active := True; except on e: EADOError do begin MessageDlg('Error while doing query', mtError, [mbOK], 0); Exit; end; end; for i := 0 to adoQry.RecordCount - 1 do begin cmbCnty.Items.Add(adoQry.Fields[1].AsString); adoQry.Next end; FreeAndNil(adoConn); FreeAndNil(adoQry);
In case dbGrid, I use StringGrid and it works for me. However, sometimes I would like to use dbGrid, but not sure how to keep a content with the close connection to the database (after retrieving the content, of course) Any suggestions, examples would appreciated.
Chris
You can populate a TClientDataSet with your Query ResultSet, and then link the TClientDataSet to the TDBGrid.