#1 2023-08-14 06:29:20

anzhi
Member
Registered: 2023-08-14
Posts: 4

dbgrid component not display data record

I am trying to use DBgrid to display Mormot query result, but DBgrid Component not show data records ,the code is as follows:

  fTable := Client.MultiFieldValues(TOrmUser, 'Name,Desc');
  DataSource := TDataSource.Create(Self);
  fDataSet := TOrmTableDataSet.Create(Self, fTable);
  DataSource.DataSet := fDataSet;
  DBGrid1.DataSource := DataSource;

the reason is why , how to fix?

Offline

#2 2023-08-14 14:30:58

tbo
Member
Registered: 2015-04-20
Posts: 328

Re: dbgrid component not display data record

anzhi wrote:

the reason is why , how to fix

I don't use Delphi's TDBGrid and can't say anything specific about it, with DevExpress TcxLookupComboBox it is solved like this:

procedure TcxLookupComboBox.InitLookupData(pmOrmTable: TOrmTable; const pmcKeyFieldNames: String; const pmcListFieldNames: String);
var
  dataSource: TDataSource;
begin
  if pmOrmTable = Nil then Exit; //=>
  if pmcKeyFieldNames = '' then Exit; //=>
  if pmcListFieldNames = '' then Exit; //=>

  dataSource := TDataSource.Create(Self);
  dataSource.DataSet := TOrmTableDataSet.CreateOwnedTable(Self, pmOrmTable);

  dataSource.DataSet.DisableControls;
  try
    ActiveProperties.ListSource := dataSource;
    ActiveProperties.KeyFieldNames := pmcKeyFieldNames;
    ActiveProperties.ListFieldNames := pmcListFieldNames;
  finally
    dataSource.DataSet.EnableControls;
  end;
end;

With best regards
Thomas

Offline

#3 2023-08-14 15:13:01

tbo
Member
Registered: 2015-04-20
Posts: 328

Re: dbgrid component not display data record

I have successfully tested it with this minimal demo:

var
  dataSource: TDataSource;
begin
  dataSource := TDataSource.Create(Self);
  dataSource.DataSet := TOrmTableDataSet.CreateFromJson(Self, StringFromFile(MakePath([Executable.ProgramFilePath, 'NBATeams.json'])));
  DBGrid.DataSource := dataSource;

Call the following function at the end to see how many records are loaded:

ShowMessage(dataSource.DataSet.RecordCount.ToString);

With best regards
Thomas

Last edited by tbo (2023-08-14 15:13:23)

Offline

#4 2023-08-15 01:21:20

anzhi
Member
Registered: 2023-08-14
Posts: 4

Re: dbgrid component not display data record

thanks very much!

i try to test it with json data , it  work !

i try to test it by using TOrmTableToGrid, it also work !

procedure TFmUser.LoadDataFromGrid;
var
  fTable: TOrmTable;
  fGrid : TOrmTableToGrid;
  DGrid: TDrawGrid;
begin
  Model := CreateUserModel;
  DGrid := TDrawGrid.Create(Self);
  DGrid.Parent := Panel6;
  Client := TDBClient.Create(Model, nil, 'lh.db', TRestServerDB, false, '');
  Client.Server.Server.CreateMissingTables;
  fTable := Client.MultiFieldValues(TOrmUser, 'Name,Desc');
  fGrid := TOrmTableToGrid.Create(DGrid, fTable, nil); 

But it did not work with dbgrid which DataSource.Dataset has data records, i guess whether the Client.MultiFieldValues result has some show question

Offline

#5 2023-08-15 14:06:00

tbo
Member
Registered: 2015-04-20
Posts: 328

Re: dbgrid component not display data record

anzhi wrote:

But it did not work with dbgrid which DataSource.Dataset has data records, i guess whether the Client.MultiFieldValues result has some show question

Delphi 11.3
mORMot2, Commit 5750 (06ac0fe)

This example works for me:

type
  TOrmItem = class(TOrm)
  private
    FName: RawUtf8;
    FDesc: RawUtf8;
  published
    property Name: RawUtf8
      read FName write FName;
    property Desc: RawUtf8
      read FDesc write FDesc;
  end;

begin
  var dbFileName: TFileName := MakePath([Executable.ProgramFilePath, 'ItemDB.db']);
  var restServer: TRestServerDB := TRestServerDB.CreateWithOwnModel([TOrmItem], dbFileName, False);
  try
    restServer.DB.Synchronous := smNormal;
    restServer.DB.LockingMode := lmExclusive;
    restServer.Server.CreateMissingTables(0, [itoNoAutoCreateGroups, itoNoAutoCreateUsers]);

    var ormItem: TOrmItem := TOrmItem.Create;
    try
      for var i: Integer := 0 to 99 do
      begin
        ormItem.Name := StringToUtf8(i.ToString);
        restServer.Add(ormItem, True);
      end;
    finally
      ormItem.Free;
    end;

    FreeAndNil(DBGrid.DataSource);
    var ormTable: TOrmTable := restServer.MultiFieldValues(TOrmItem, 'Name, Desc');
    if ormTable.RowCount > 0 then
    begin
      DBGrid.DataSource := TDataSource.Create(DBGrid);
      DBGrid.DataSource.DataSet :=  TOrmTableDataSet.Create(DBGrid, ormTable);
    end;
  finally
    restServer.Free;
  end;
end;

initialization
  var logFamily: TSynLogFamily := TSynLog.Family;
  logFamily.Level := LOG_VERBOSE;

With best regards
Thomas

Offline

#6 2023-08-19 16:52:49

anzhi
Member
Registered: 2023-08-14
Posts: 4

Re: dbgrid component not display data record

thanks very much, i trying to test it as above code, but i found some question,
the database which i select is sqlite, the data record as follow:
------------------------------------
     |    Name    |    Desc   |
-------------------------------------
     |       1       |      hello  |

but the DBGrid display data as follow

------------------------------------
     |    Name    |    Desc   |
-------------------------------------
     |       1       |      h  |

i found the "hello" field value only display the first letter,  this is why?
thanks!

Offline

#7 2023-08-19 20:49:28

tbo
Member
Registered: 2015-04-20
Posts: 328

Re: dbgrid component not display data record

anzhi wrote:

i found the "hello" field value only display the first letter,  this is why?

Please tell us the Delphi and mORMot version you are using.

Create a new project. Put a DBGrid and a Button on the Form. Add the following Units in the uses section.

uses
  ...
  mormot.core.base,
  mormot.core.data,
  mormot.core.text,
  mormot.core.unicode,
  mormot.core.log,
  mormot.core.os,
  mormot.orm.base,
  mormot.orm.core,
  mormot.rest.sqlite3,
  mormot.db.rad.ui.orm,
  mormot.db.raw.sqlite3,
  mormot.db.raw.sqlite3.static;

Paste the source code from my example. From the "begin" all in the OnClick event of the Button. Also add the Desc after the Name property when filling the database like in the source code below.

ormItem.Desc := 'Description ' + StringToUtf8(i.ToString);

If there are still problems, you can insert the following source code after assigning the DataSet and explicitly create the columns of the grid.

DBGrid.DataSource.DataSet.DisableControls;
try
  var gridCol: TColumn;
  gridCol := DBGrid.Columns.Add;
  gridCol.FieldName := 'Name';
  gridCol.Width := 50;

  gridCol := DBGrid.Columns.Add;
  gridCol.FieldName := 'Desc';
  gridCol.Width := 200;
finally
  DBGrid.DataSource.DataSet.EnableControls;
end;

With best regards
Thomas

Last edited by tbo (2023-08-19 20:50:27)

Offline

#8 2023-08-20 02:37:27

anzhi
Member
Registered: 2023-08-14
Posts: 4

Re: dbgrid component not display data record

tbo wrote:
anzhi wrote:

i found the "hello" field value only display the first letter,  this is why?

Please tell us the Delphi and mORMot version you are using.

I test it with above code  using Lazarus2.2.6 and mormot2,but it do not work!

I also test it  with SqlQuery, SQLlite3Connection , it works, the code as follows

procedure TFmUser.LoadDataFromSQL;
begin
    SQLQuery1.Close;
    SQLQuery1.SQL.Clear;
    SQLQuery1.SQL.Text:= 'SELECT * from user';
    DBGrid1.Options := DBGrid1.Options + [dgDisplayMemoText];
    SQLQuery1.Open;

end; 

thanks

Last edited by anzhi (2023-08-20 07:27:25)

Offline

Board footer

Powered by FluxBB