How do I get the results of a MySQL Query to display in a cxGrid?

I’m using Delphi 5, and I want to display the results of a MySQL query in a cxGrid. I have the cxGrid, cxGridLevel, and cxGridDBTableView set up the way they default to when added to a form. The DataController.DataSource for the cxGridDBTableView is a TDataSource named DSNewKits with its DataSet set to a tMySQLQuery named NewKitsQry.

When it’s time to display the data, I set the Enabled property of DSNewKits to false, run the Close method of NewKitsQry, then set the Enabled property of DSNewKits to true, set the SQL.Text property of NewKitsQry, and run the Open method NewKitsQry. I then have a message dialog display the number of results in the query, and there are 408, so I know the query is working properly. The grid displays rows & columns with lines in between, no "No data to display" message, but all the cells are blank.

I have tried doing these steps in a different order, commenting out some of them, etc., but nothing causes the data to display. I’m sure I’m missing something simple &/or obvious, but I was unable to find an example online. Thank you for any help you can give!

(Edited to include code) Here is my code:

 interface  uses   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,   cxGridLevel, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,   cxClasses, cxControls, cxGridCustomView, cxGrid, ExtCtrls, Db,   mySQLDbTables, ovcbase, ovcfiler, ovcstore,   gECCConst,   StdCtrls;  type   Tf_cw_cxgrid_db = class(TForm)     WebCatDB: TmySQLDatabase;     NewKitsQry: TmySQLQuery;     DSNewKits: TDataSource;     Panel1: TPanel;     cxNewKitsGrid: TcxGrid;     cxNewKitsGridDBTableView1: TcxGridDBTableView;     cxNewKitsGridDBTableView1CompanyID: TcxGridDBColumn;     cxNewKitsGridDBTableView1RegionID: TcxGridDBColumn;     cxNewKitsGridDBTableView1BranchID: TcxGridDBColumn;     cxNewKitsGridDBTableView1CustID: TcxGridDBColumn;     cxNewKitsGridDBTableView1Username: TcxGridDBColumn;     cxNewKitsGridDBTableView1KitID: TcxGridDBColumn;     cxNewKitsGridDBTableView1KitComment: TcxGridDBColumn;     cxNewKitsGridDBTableView1KitEffDate: TcxGridDBColumn;     cxNewKitsGridDBTableView1KitExpDate: TcxGridDBColumn;     cxNewKitsGridDBTableView1KitNote: TcxGridDBColumn;     cxNewKitsGridDBTableView1KitType: TcxGridDBColumn;     cxNewKitsGridDBTableView1KitFlags: TcxGridDBColumn;     cxNewKitsGridDBTableView1KitTab: TcxGridDBColumn;     cxNewKitsGridDBTableView1KitAddDate: TcxGridDBColumn;     cxNewKitsGridDBTableView1KitEditedDate: TcxGridDBColumn;     cxNewKitsGridDBTableView1Line: TcxGridDBColumn;     cxNewKitsGridDBTableView1ProductID: TcxGridDBColumn;     cxNewKitsGridDBTableView1MfgProdID: TcxGridDBColumn;     cxNewKitsGridDBTableView1PartNum: TcxGridDBColumn;     cxNewKitsGridDBTableView1ProductDesc: TcxGridDBColumn;     cxNewKitsGridDBTableView1Qty: TcxGridDBColumn;     cxNewKitsGridDBTableView1RefQty: TcxGridDBColumn;     cxNewKitsGridDBTableView1UnitPrice: TcxGridDBColumn;     cxNewKitsGridDBTableView1UnitPriceType: TcxGridDBColumn;     cxNewKitsGridDBTableView1UofM: TcxGridDBColumn;     cxNewKitsGridDBTableView1PSUofM: TcxGridDBColumn;     cxNewKitsGridDBTableView1UNSPSC: TcxGridDBColumn;     cxNewKitsGridDBTableView1ItemNote: TcxGridDBColumn;     cxNewKitsGridDBTableView1ItemGroupID: TcxGridDBColumn;     cxNewKitsGridDBTableView1ItemDrpDnHdr: TcxGridDBColumn;     cxNewKitsGridDBTableView1ItemSection: TcxGridDBColumn;     cxNewKitsGridDBTableView1ItemFlags: TcxGridDBColumn;     cxNewKitsGridDBTableView1ItemAddDate: TcxGridDBColumn;     cxNewKitsGridDBTableView1ItemEditedDate: TcxGridDBColumn;     cxNewKitsGrid1Level1: TcxGridLevel;     RegistryStore: TOvcRegistryStore;     userqry: TmySQLQuery;     Button1: TButton;     procedure FormShow(Sender: TObject);     procedure Button1Click(Sender: TObject);   private     { Private declarations }   public     { Public declarations }   end;  var   f_cw_cxgrid_db: Tf_cw_cxgrid_db;  implementation  {$R *.DFM}  procedure Tf_cw_cxgrid_db.FormShow(Sender: TObject); var   Server : String;   User   : String;   Passwd : String;   DBName : String;   Button : Integer;   WCPath : String; begin   RegistryStore.Open;   Server     := RegistryStore.ReadString( 'SBAdmin', 'Host',     '');   User       := RegistryStore.ReadString( 'SBAdmin', 'User',     '');   Passwd     := RegistryStore.ReadString( 'SBAdmin', 'Password', '');   DBName     := RegistryStore.ReadString( 'SBAdmin', 'Database', WebCatDefUserDBName );  { caw 7-24-20 }   RegistryStore.Close;    WebCatDB.Host              := Server;   WebCatDB.UserName          := User;   WebCatDB.UserPassword      := Passwd;   WebCatDB.DatabaseName      := 'WEBCAT_' + DBName; end;  procedure Tf_cw_cxgrid_db.Button1Click(Sender: TObject); begin   DSNewKits.Enabled := False;   NewKitsQry.Close;   try     DSNewKits.Enabled := True;     NewKitsQry.SQL.Text := 'SELECT KitHdrs.CompanyID, KitHdrs.RegionID, KitHdrs.BranchID, KitHdrs.CustID, ' +                            'KitHdrs.Username, KitHdrs.KitID, KitHdrs.KitComment, KitHdrs.KitEffDate, ' +                            'KitHdrs.KitExpDate, KitHdrs.KitNote, KitHdrs.KitType, KitHdrs.KitFlags, KitHdrs.KitTab, ' +                            'KitHdrs.KitAddDate, KitHdrs.KitEditedDate, KitLines.Line, KitLines.ProductID, ' +                            'KitLines.MfgProdID, KitLines.PartNum, KitLines.ProductDesc, KitLines.Qty, ' +                            'KitLines.RefQty, KitLines.UnitPrice, KitLines.UnitPriceType, KitLines.UofM, ' +                            'KitLines.PSUofM, KitLines.UNSPSC, KitLines.ItemNote, KitLines.ItemGroupID, ' +                            'KitLines.ItemDrpDnHdr, KitLines.ItemSection, KitLines.ItemFlags, ' +                            'KitLines.ItemAddDate, KitLines.ItemEditedDate ' +                            'FROM KitHdrs JOIN KitLines ON KitHdrs.KitSeq = KitLines.KitSeq;';     NewKitsQry.Open;     cxNewKitsGridDBTableView1.DataController.CreateAllItems;      MessageDlg( 'There are ' + IntToStr( NewKitsQry.RecordCount ) + ' records.', mtInformation, [mbOK], 0 );   except     MessageDlg( 'There was an error displaying the kit databases.', mtError, [mbOK], 0 );   end; end;  end. 
Add Comment
1 Answer(s)

Although the cxGrid is a fine component, the fact that it has so many deeply-nested properties can make it quite daunting to set up a cxGrid from scratch.

I created the example below to show how to create and set up a cxGrid entirely in code so that you can easily see the bare minimum which needs to be done. It uses a TClientDataSet, which is populated in code, to supply the grid’s data so that the example is completely self-contained. It would be trivial to adapt it to an existing MySql dataset.

type   TForm1 = class(TForm)     CDS1: TClientDataSet;     DS1: TDataSource;     DBNavigator1: TDBNavigator;     Button1: TButton;     procedure FormCreate(Sender: TObject);   private   protected   public     cxGrid : TcxGrid;     cxLevel : TcxGridLevel;     cxView : TcxGridDBTableView;   end; [...] // This is a utility function to create TFields in code function CreateField(AFieldClass : TFieldClass; AOwner : TComponent; ADataSet : TDataSet; AFieldName, AName : String; ASize : Integer; AFieldKind : TFieldKind) : TField; begin   Result := AFieldClass.Create(AOwner);   Result.FieldKind := AFieldKind;   Result.FieldName := AFieldName;   Result.Name := AName;   Result.Size := ASize;   Result.DataSet := ADataSet; end;  procedure TForm1.FormCreate(Sender: TObject); var   i : Integer;   Field : TField; begin   //  All the code to set up the cxGrid is in this event handler      //  First, create the Fields of the ClientDataSet   Field := CreateField(TIntegerField, Self, CDS1, 'ID', 'CDS1ID', 0, fkData);   Field := CreateField(TIntegerField, Self, CDS1, 'Qty', 'CDS1Qty', 0, fkData);   Field := CreateField(TCurrencyField, Self, CDS1, 'UnitPrice', 'CDS1UnitPrice', 0, fkData);   CDS1.CreateDataSet;    CDS1.IndexFieldNames := 'ID';    //  Next, populate the CDS with a few records   CDS1.InsertRecord([1, 1, 1]);   CDS1.InsertRecord([2, 2, 5]);   CDS1.InsertRecord([3, 3, 6]);    CDS1.First;    DS1.DataSet := CDS1;    //  Now, create a cxGrid to display the CDS data   cxGrid := TcxGrid.Create(Self);   cxGrid.Parent := Self;   cxGrid.Width := 400;    cxLevel := cxGrid.Levels.Add;   cxLevel.Name := 'Firstlevel';    cxView := cxGrid.CreateView(TcxGridDBTableView) as TcxGridDBTableView;   cxView.Name := 'ATableView';   cxView.DataController.KeyFieldNames := 'ID';    cxLevel.GridView := cxView;   cxView.DataController.DataSource := DS1;   cxView.DataController.CreateAllItems; end; 
Answered on September 1, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.