如何获取 MySQL 查询的结果以显示在 cxGrid 中?
How do I get the results of a MySQL Query to display in a cxGrid?
我正在使用 Delphi 5,我想在 cxGrid 中显示 MySQL 查询的结果。我将 cxGrid、cxGridLevel 和 cxGridDBTableView 设置为添加到表单时的默认方式。 cxGridDBTableView 的 DataController.DataSource 是一个名为 DSNewKits 的 TDataSource,其 DataSet 设置为一个名为 NewKitsQry 的 tMySQLQuery。
显示数据的时候,我把DSNewKits的Enabled属性设置为false,运行NewKitsQry的Close方法,然后把DSNewKits的Enabled属性设置为true,设置NewKitsQry的SQL.Text属性,NewKitsQry的Open方法运行。然后我有一个消息对话框显示查询中的结果数,有 408 个,所以我知道查询工作正常。网格显示行和列之间有线,没有“没有数据显示”消息,但所有单元格都是空白的。
我试过以不同的顺序执行这些步骤,注释掉其中的一些,等等,但没有任何原因导致数据显示。我确定我遗漏了一些简单和/或明显的东西,但我无法在网上找到示例。感谢您提供的任何帮助!
(编辑以包含代码)这是我的代码:
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', '127.0.0.1');
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.
虽然 cxGrid 是一个很好的组件,但事实上它有这么多 deeply-nested
属性可能会使从头开始设置 cxGrid 变得非常艰巨。
我创建了下面的示例来展示如何完全在代码中创建和设置 cxGrid
这样您就可以轻松地看到需要完成的最低限度。它使用 TClientDataSet,
这是在代码中填充的,以提供网格的数据,以便示例完全 self-contained。使其适应现有的 MySql 是微不足道的
数据集。
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;
我正在使用 Delphi 5,我想在 cxGrid 中显示 MySQL 查询的结果。我将 cxGrid、cxGridLevel 和 cxGridDBTableView 设置为添加到表单时的默认方式。 cxGridDBTableView 的 DataController.DataSource 是一个名为 DSNewKits 的 TDataSource,其 DataSet 设置为一个名为 NewKitsQry 的 tMySQLQuery。
显示数据的时候,我把DSNewKits的Enabled属性设置为false,运行NewKitsQry的Close方法,然后把DSNewKits的Enabled属性设置为true,设置NewKitsQry的SQL.Text属性,NewKitsQry的Open方法运行。然后我有一个消息对话框显示查询中的结果数,有 408 个,所以我知道查询工作正常。网格显示行和列之间有线,没有“没有数据显示”消息,但所有单元格都是空白的。
我试过以不同的顺序执行这些步骤,注释掉其中的一些,等等,但没有任何原因导致数据显示。我确定我遗漏了一些简单和/或明显的东西,但我无法在网上找到示例。感谢您提供的任何帮助!
(编辑以包含代码)这是我的代码:
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', '127.0.0.1');
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.
虽然 cxGrid 是一个很好的组件,但事实上它有这么多 deeply-nested 属性可能会使从头开始设置 cxGrid 变得非常艰巨。
我创建了下面的示例来展示如何完全在代码中创建和设置 cxGrid 这样您就可以轻松地看到需要完成的最低限度。它使用 TClientDataSet, 这是在代码中填充的,以提供网格的数据,以便示例完全 self-contained。使其适应现有的 MySql 是微不足道的 数据集。
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;