Recent

Author Topic: Populating a DBGrid or DBEdit  (Read 1653 times)

heebiejeebies

  • Full Member
  • ***
  • Posts: 127
Populating a DBGrid or DBEdit
« on: September 06, 2021, 10:22:21 am »
Hi all,

Well, it seems every time I try something new I have to ask first to get it to work!  :-[  Maybe one day I'll figure something out myself.  Latest clumps of hair being torn out have been caused by DBGrid and DBEdit refusing to populate.  I have lots of database functions in my program that work perfectly, so I know the basics of hooking up the various components.  I have a connection, transaction, datasource and query all connected to each other.  The DBGrid and DBEdit have the relevant datasource set.  I have a procedure which I copied from the only datagrid tutorial I could find online, which follows:

Code: Pascal  [Select][+][-]
  1.   UserFileConnection.Open;
  2.   UserFileTransaction.Active:= True;
  3.   Userdatasource.Enabled :=true;
  4.   UserFileLoad.SQL.Text:=('SELECT * FROM IssueTable');
  5.   Userdatasource.DataSet:= UserFileLoad;
  6.   PPDatagrid.DataSource :=Userdatasource;
  7.   PPDatagrid.Autofillcolumns :=true;
  8.   DBEdit1.Datafield :='IssueName';
  9.   UserFileTransaction.Commit;
  10.   UserFileLoad.Open;

Have tried putting the DB commands in different orders and so on.  Have tried filtering the select statement by a single rowid.  The above code at least makes it load one lot of blank data, but nothing else.  I get one row of blanks in the datagrid, and the DBEDIT populates with the text '(MEMO)'.  If I insert columns into the datagrid matching the fields of the table then I can get '(MEMO)' to appear there too. Any ideas?
Fedora 38/Lazarus 2.2.4- FPC 3.3.1

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: Populating a DBGrid or DBEdit
« Reply #1 on: September 06, 2021, 10:32:33 am »
In order to display a memo field use a TDBMemo control rather than a TDBEdit.

balazsszekely

  • Guest
Re: Populating a DBGrid or DBEdit
« Reply #2 on: September 06, 2021, 10:34:27 am »
Hi heebiejeebies,

Quote
Well, it seems every time I try something new I have to ask first to get it to work!  :-[  Maybe one day I'll figure something out myself.  Latest clumps of hair being torn out have been caused by DBGrid and DBEdit refusing to populate.  I have lots of database functions in my program that work perfectly, so I know the basics of hooking up the various components.  I have a connection, transaction, datasource and query all connected to each other.  The DBGrid and DBEdit have the relevant datasource set.  I have a procedure which I copied from the only datagrid tutorial I could find online, which follows:

Code: Pascal  [Select][+][-]
  1.   UserFileConnection.Open;
  2.   UserFileTransaction.Active:= True;
  3.   Userdatasource.Enabled :=true;
  4.   UserFileLoad.SQL.Text:=('SELECT * FROM IssueTable');
  5.   Userdatasource.DataSet:= UserFileLoad;
  6.   PPDatagrid.DataSource :=Userdatasource;
  7.   PPDatagrid.Autofillcolumns :=true;
  8.   DBEdit1.Datafield :='IssueName';
  9.   UserFileTransaction.Commit;
  10.   UserFileLoad.Open;

Have tried putting the DB commands in different orders and so on.  Have tried filtering the select statement by a single rowid.  The above code at least makes it load one lot of blank data, but nothing else.  I get one row of blanks in the datagrid, and the DBEDIT populates with the text '(MEMO)'.  If I insert columns into the datagrid matching the fields of the table then I can get '(MEMO)' to appear there too. Any ideas?

Looks good! The only thing is missing, is the link between the query(UserFileLoad) and the connection(UserFileConnection):
Code: Pascal  [Select][+][-]
  1. //...
  2. UserFileLoad.Database := UserFileConnction; //<-This line
  3. UserFileLoad.Open

heebiejeebies

  • Full Member
  • ***
  • Posts: 127
Re: Populating a DBGrid or DBEdit
« Reply #3 on: September 06, 2021, 11:06:41 am »
In order to display a memo field use a TDBMemo control rather than a TDBEdit.

Thanks!  Still doesn't work though.  :(  Just appears blank, doesn't even give the '(Memo)' like the DBEdit does.
Fedora 38/Lazarus 2.2.4- FPC 3.3.1

heebiejeebies

  • Full Member
  • ***
  • Posts: 127
Re: Populating a DBGrid or DBEdit
« Reply #4 on: September 06, 2021, 11:10:48 am »
Hi heebiejeebies,

Looks good! The only thing is missing, is the link between the query(UserFileLoad) and the connection(UserFileConnection):
Code: Pascal  [Select][+][-]
  1. //...
  2. UserFileLoad.Database := UserFileConnction; //<-This line
  3. UserFileLoad.Open

Thanks GetMem!  I have the database already linked up through the object inspector.  It works this way for all my other queries so I didn't bother putting it in code.  Actually there are probably a few lines in the code I posted that are redundant due to the properties already being set in the object inspector, but I decided to follow the tutorial exactly just to be sure.  Anyway, I tried adding in your database line but I still got the same result. :(
Fedora 38/Lazarus 2.2.4- FPC 3.3.1

balazsszekely

  • Guest
Re: Populating a DBGrid or DBEdit
« Reply #5 on: September 06, 2021, 11:20:04 am »
Does your query return any data? Please put a button to your form, then:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   UserFileLoad.First;
  4.   while not UserFileLoad.EOF do
  5.   begin
  6.     ShowMessage(UserFileLoad.FieldByName('IssueName').AsString);
  7.     UserFileLoad.Next;
  8.   end;
  9. end;
If yes, then I have no idea what is the problem, you should attach your program to your next post.

heebiejeebies

  • Full Member
  • ***
  • Posts: 127
Re: Populating a DBGrid or DBEdit
« Reply #6 on: September 06, 2021, 11:50:30 am »
Yes it does return the data from that table.  Thanks for your efforts!  :) I'll see if anyone else has any ideas and if I have no luck I'll attach it, but it'll be a bit of effort just to extract that part since it's part of a large project.
Fedora 38/Lazarus 2.2.4- FPC 3.3.1

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Re: Populating a DBGrid or DBEdit
« Reply #7 on: September 10, 2021, 07:58:58 am »
First try to display the content of query just linking DB components. Not using codes.

heebiejeebies

  • Full Member
  • ***
  • Posts: 127
Re: Populating a DBGrid or DBEdit
« Reply #8 on: September 23, 2021, 12:45:54 pm »
First try to display the content of query just linking DB components. Not using codes.

I did - it didn't work. :(
Fedora 38/Lazarus 2.2.4- FPC 3.3.1

 

TinyPortal © 2005-2018