简体   繁体   English

如何获取 MySQL 查询的结果以显示在 cxGrid 中?

[英]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.我正在使用 Delphi 5,我想在 cxGrid 中显示 MySQL 查询的结果。 I have the cxGrid, cxGridLevel, and cxGridDBTableView set up the way they default to when added to a form.我将 cxGrid、cxGridLevel 和 cxGridDBTableView 设置为添加到表单时的默认方式。 The DataController.DataSource for the cxGridDBTableView is a TDataSource named DSNewKits with its DataSet set to a tMySQLQuery named NewKitsQry. cxGridDBTableView 的 DataController.DataSource 是一个名为 DSNewKits 的 TDataSource,其 DataSet 设置为名为 NewKitsQry 的 tMySQLQuery。

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.当需要显示数据时,我将DSNewKits的Enabled属性设置为false,运行NewKitsQry的Close方法,然后将DSNewKits的Enabled属性设置为true,设置NewKitsQry的SQL.Text属性,运行NewKitsQry的Open方法. 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.然后我有一个消息对话框显示查询中的结果数,有 408 个,所以我知道查询工作正常。 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',     '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.

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.尽管 cxGrid 是一个很好的组件,但它具有如此多的深层嵌套属性这一事实使得从头开始设置 cxGrid 变得相当艰巨。

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.我创建了下面的示例来展示如何完全在代码中创建和设置 cxGrid,以便您可以轻松地看到需要完成的最低限度。 It uses a TClientDataSet, which is populated in code, to supply the grid's data so that the example is completely self-contained.它使用代码中填充的 TClientDataSet 来提供网格数据,因此该示例是完全独立的。 It would be trivial to adapt it to an existing MySql dataset.将其适应现有的 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取查询以显示两个结果? - How do I get the query to display both results? 我如何将mysql查询的结果获取到html文本框中 - How do i get the results of a mysql query into html textbox 如何使用-initWithContentsOfURL获取MySQL查询结果? - How do I get MySQL query-results using -initWithContentsOfURL? 如何限制此MySQL查询的结果? - How do I limit the results of this MySQL Query? 如何正确地将此mySQL查询实现为PHP文件并显示结果? - How do I correctly implement this mySQL query into a PHP file and display the results? 如何通过VB建立与MySQL的连接并在表单上显示简单的查询结果? - How do I establish a connection to MySQL via VB & display simple query results on a form? 在MySQL查询上的左联接没有得到我想要的结果 - LEFT JOIN on mysql query do not get results like i want 如何从Joomla中的组件菜单项获取MySQL查询结果,并使用foreach where子句进行优化? - How do I get MySQL query results from component menu item in Joomla and refine with a foreach where clause? 我如何仅从MySQL查询中获得肯定的结果? - How do I get positive results only from my MySQL query? 我如何将mysql查询的结果显示到python中的Qradiobuttons中 - How can i display results from a mysql query into Qradiobuttons in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM