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

如何解决如何获取MySQL查询的结果以显示在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。然后,我有一个消息对话框,显示查询中的结果数,共有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,0 );
  end;
end;

end.

解决方法

尽管cxGrid是一个很好的组件,但事实是它有很多深层的嵌套 属性会使从头开始设置cxGrid变得相当艰巨。

我创建了以下示例,以展示如何完全在代码中创建和设置cxGrid 这样您就可以轻松看到需要完成的最低要求。它使用了TClientDataSet 用代码填充,以提供网格数据,从而使示例完全独立。使其适应现有的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',fkData);
  Field := CreateField(TIntegerField,'Qty','CDS1Qty',fkData);
  Field := CreateField(TCurrencyField,'UnitPrice','CDS1UnitPrice',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;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-