Recent

Author Topic: DBGrid DataSource JsonDataSet, DBGrid can't show data.  (Read 2557 times)

bird00101011

  • Newbie
  • Posts: 6
DBGrid DataSource JsonDataSet, DBGrid can't show data.
« on: June 24, 2024, 11:05:27 am »
1. JsonDataSet active is True.
2. DataSource dataset is  JsonDataSet that i created.
3. DBGrid datasource is also right.

Everything is right, when code run: JsonDataSet appendRecord([...]), DBGrid show nothing.

This is a BUG with DBGrid, DataSource, JsonDataSet Components?

Else Any one who could help this problem? >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D >:D

This must be a bug.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11645
  • FPC developer.
Re: DBGrid DataSource JsonDataSet, DBGrid can't show data.
« Reply #1 on: June 24, 2024, 11:22:04 am »
Maybe dbgrid.refresh  ?  >:D

bird00101011

  • Newbie
  • Posts: 6
Re: DBGrid DataSource JsonDataSet, DBGrid can't show data.
« Reply #2 on: June 24, 2024, 12:58:24 pm »
I have found the example from lazarus wiki.

```fpc=fpc3.2-fixed lazarus=master
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, DBCtrls, DBGrids, DB,
  fpjson, fpjsondataset;

type

  TForm1 = class(TForm)
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    procedure FormCreate(Sender: TObject);
  private
    JSONDataSet: TJSONDataSet;
  end;

var
  Form1: TForm1;

const
  // this is the sample JSON data used in the demo
  JSON_STRING = '[' +
    '{"Author ID":"409-56-7008","Last Name":"Bennet","First Name":"Abraham","Active": False},'
    + '{"Author ID":"213-46-8915","Last Name":"Green","First Name":"Marjorie","Active": True}'
    + ']';

implementation

{$R *.lfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  Data: TJSONArray;
begin
  DBGrid1.AutoFillColumns := True;

  // parse the JSON string
  Data := GetJSON(JSON_STRING) as TJSONArray;
  // create a new TJSONDataSet
  JSONDataSet := TJSONDataSet.Create(Self);
  // you'll need to create the FieldDefs manually
  // ftString requires a length specified
  JSONDataSet.FieldDefs.Add('Author ID', ftString, 11, True);
  JSONDataSet.FieldDefs.Add('Last Name', ftString, 40);
  JSONDataSet.FieldDefs.Add('First Name', ftString, 20);
  JSONDataSet.FieldDefs.Add('Active', ftBoolean);
  // set OwnsData to True, and the JSON data will be destroyed when the dataset is destroyed
  JSONDataSet.OwnsData := True;
  // the JSON data is an array of objects
  JSONDataSet.RowType := rtJSONObject;
  // assign the JSON data to the dataset
  JSONDataSet.Rows := Data;
  // activate the dataset
  JSONDataSet.Active := True;
  // assign the dataset to the datasource
  DataSource1.DataSet := JSONDataSet;

  DBGrid1.Refresh; {Refresh last. DBGrid1 show nothing. bug must be.}
end;

end.
```

Wiki url is: https://wiki.freepascal.org/fpjsondataset

Also there is something wrong with DBGrid, DataSource and JSonDataSet components. This demo, DBGrid1 show nothing.
 >:(

bird00101011

  • Newbie
  • Posts: 6
Re: DBGrid DataSource JsonDataSet, DBGrid can't show data.
« Reply #3 on: June 24, 2024, 01:10:56 pm »
I have upload the project.

It's very important for lazarus users. wait for your reply.

dseligo

  • Hero Member
  • *****
  • Posts: 1282
Re: DBGrid DataSource JsonDataSet, DBGrid can't show data.
« Reply #4 on: June 24, 2024, 01:27:04 pm »
Don't put this code in FormCreate event.
I changed your project: added button and put this code in OnClick event of button.

Check attached project.

dseligo

  • Hero Member
  • *****
  • Posts: 1282
Re: DBGrid DataSource JsonDataSet, DBGrid can't show data.
« Reply #5 on: June 24, 2024, 01:30:45 pm »
If you have to open dataset when starting program, you can put your code in OnActivate event of the form, like this:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormActivate(Sender: TObject);
  2. const FirstTime: Boolean = True;
  3. var
  4.   Data: TJSONArray;
  5. begin
  6.   If FirstTime then
  7.   begin
  8.     FirstTime := False;
  9.  
  10.     DBGrid1.AutoFillColumns := True;
  11.     DBGrid1.DataSource := DataSource1;
  12.      DataSource1.Edit;
  13.     // parse the JSON string
  14.     Data := GetJSON(JSON_STRING) as TJSONArray;
  15.     // create a new TJSONDataSet
  16.     JSONDataSet := TJSONDataSet.Create(Self);
  17.     // you'll need to create the FieldDefs manually
  18.     // ftString requires a length specified
  19.     JSONDataSet.FieldDefs.Add('Author ID', ftString, 11, True);
  20.     JSONDataSet.FieldDefs.Add('Last Name', ftString, 40);
  21.     JSONDataSet.FieldDefs.Add('First Name', ftString, 20);
  22.     JSONDataSet.FieldDefs.Add('Active', ftBoolean);
  23.     // set OwnsData to True, and the JSON data will be destroyed when the dataset is destroyed
  24.     JSONDataSet.OwnsData := True;
  25.     // the JSON data is an array of objects
  26.     JSONDataSet.RowType := rtJSONObject;
  27.     // assign the JSON data to the dataset
  28.     JSONDataSet.Rows := Data;
  29.     // activate the dataset
  30.     JSONDataSet.Active := True;
  31.     // assign the dataset to the datasource
  32.     DataSource1.DataSet := JSONDataSet;
  33.  
  34.     DBGrid1.Refresh;
  35.   end;
  36. end;

BrunoK

  • Hero Member
  • *****
  • Posts: 505
  • Retired programmer
Re: DBGrid DataSource JsonDataSet, DBGrid can't show data.
« Reply #6 on: June 24, 2024, 03:16:23 pm »
I have upload the project.

It's very important for lazarus users. wait for your reply.
Simply, you forgot to bind your FormCreate procedure to the Form1.OnCreate in the object inspector.

One done, your sample data shows OK. ;-)

dseligo

  • Hero Member
  • *****
  • Posts: 1282
Re: DBGrid DataSource JsonDataSet, DBGrid can't show data.
« Reply #7 on: June 24, 2024, 03:25:23 pm »
I have upload the project.

It's very important for lazarus users. wait for your reply.
Simply, you forgot to bind your FormCreate procedure to the Form1.OnCreate in the object inspector.

One done, your sample data shows OK. ;-)

You are correct. :)
When I opened his project, I didn't check this, I just assumed it is something with early creation of objects.

Zvoni

  • Hero Member
  • *****
  • Posts: 2464
Re: DBGrid DataSource JsonDataSet, DBGrid can't show data.
« Reply #8 on: June 24, 2024, 03:30:52 pm »
This must be a bug.
It is. It's a bug between brain and fingers.....
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

BrunoK

  • Hero Member
  • *****
  • Posts: 505
  • Retired programmer
Re: DBGrid DataSource JsonDataSet, DBGrid can't show data.
« Reply #9 on: June 24, 2024, 06:28:39 pm »
This must be a bug.
It is. It's a bug between brain and fingers.....
Except for the excessive grump'ish emoticons, the original poster, contrary to many Heroes around here, submitted a compilable project. :-)
 

bird00101011

  • Newbie
  • Posts: 6
Re: DBGrid DataSource JsonDataSet, DBGrid can't show data.
« Reply #10 on: June 24, 2024, 11:09:32 pm »
Very thanks , so manythanks to everyboy.

I have a good English. >:D

 :-*

jesusr

  • Sr. Member
  • ****
  • Posts: 496
Re: DBGrid DataSource JsonDataSet, DBGrid can't show data.
« Reply #11 on: June 25, 2024, 02:47:39 am »
I have upload the project.

It's very important for lazarus users. wait for your reply.


The problem with this project is that the FormCreate event handler is not connected, in the object inspector double click Form1 (or double click the value section in the OnCreate event) it should connect the existing TForm1.FormCreate code.

This looks like a copy and paste problem, the linked wiki article doesn't instruct the user to connect the forms OnCreate even handler.

Zvoni

  • Hero Member
  • *****
  • Posts: 2464
Re: DBGrid DataSource JsonDataSet, DBGrid can't show data.
« Reply #12 on: June 25, 2024, 08:03:26 am »
This looks like a copy and paste problem, the linked wiki article doesn't instruct the user to connect the forms OnCreate even handler.
When going for your driving license, nobody is instructing you how to start the engine, either, or to check oil and fuel.
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

jesusr

  • Sr. Member
  • ****
  • Posts: 496
Re: DBGrid DataSource JsonDataSet, DBGrid can't show data.
« Reply #13 on: June 25, 2024, 09:06:23 pm »
When going for your driving license, nobody is instructing you how to start the engine, either, or to check oil and fuel.
A far fetched analogy, because almost everybody knows about it. It don't scale well when you are learning a new technology. It would better if the user asked nicely though, but free speech is also important.

Zvoni

  • Hero Member
  • *****
  • Posts: 2464
Re: DBGrid DataSource JsonDataSet, DBGrid can't show data.
« Reply #14 on: June 26, 2024, 08:06:48 am »
It don't scale well when you are learning a new technology.
A user coding a Database-App with JSON involved....
[SARCASM ON]
Yeah. Rrrriiight......
A rookie learning a new technology....
[SARCASM OFF]
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018