Recent

Author Topic: Displaying Data from DBGrid into DBFields - Error Hooking up fields  (Read 1274 times)

mangakissa

  • Hero Member
  • *****
  • Posts: 921
Re: Displaying Data from DBGrid into DBFields - Error Hooking up fields
« Reply #15 on: April 26, 2019, 09:48:10 am »
I dont get it. You have a DBGrid, a DBCombobox, a few DBEdit and t Memo on form. Each line in dbgrid is showed in these components. So why want you to click on a cell. The option you have set in DBGrid is dgRowselect. It meens you can't edit a cell.
Quote
MY ONLY QUESTION FOR NOW IS. After displaying another window(Form), edit the RTF in RichMemo...
How do I get it back to the cell where I clicked the "Button" in the cell???
The cursor on DBGrid didn't moved when you calling a new form.

I don't undersand what you mean.
How did you call the selected recod in the new form?
Lazarus 1.84 (32b) / FPC 3.0.4
Windows 10

pixelink

  • Hero Member
  • *****
  • Posts: 943
    • Pixelink Media
Re: Displaying Data from DBGrid into DBFields - Error Hooking up fields
« Reply #16 on: April 26, 2019, 11:52:00 am »
I dont get it. You have a DBGrid, a DBCombobox, a few DBEdit and t Memo on form. Each line in dbgrid is showed in these components. So why want you to click on a cell. The option you have set in DBGrid is dgRowselect. It meens you can't edit a cell.
Quote
MY ONLY QUESTION FOR NOW IS. After displaying another window(Form), edit the RTF in RichMemo...
How do I get it back to the cell where I clicked the "Button" in the cell???
The cursor on DBGrid didn't moved when you calling a new form.

I don't undersand what you mean.
How did you call the selected recod in the new form?

You must not of read the thread very well...

the reason I was using the DB controls is because the DB components were not working.

I have since fixed those.
I am not using DB Controls except one I need to store RTf code. Because LAZ doesn't to my knowledge have a DB RichEdit Control that can be hooked up to the SQL components.
SO, I am using a DB Memo as a go between storing the RTF data in Db and using RichMemo to edit the RTF.
I use code to transfer the RTF from DB Grid to DBMemo then onto RichMemo.

So, I have it setup so the user edits the grid directly.

Things have changed, go back and re-read the entire thread,.
Can't Type - Forgetful - Had Stroke = Forgive this old man!


Pixelink Media •  Pixelink Forum
LAZ 2.02 • Win10 • 16G RAM • Nivida GForce RTX 2060

pixelink

  • Hero Member
  • *****
  • Posts: 943
    • Pixelink Media
Re: Displaying Data from DBGrid into DBFields - Error Hooking up fields
« Reply #17 on: April 26, 2019, 02:03:20 pm »
Quick question.

Everything is working fine.

Up to this point, I have to use a DBMemo to store RTF code, because as far as I know, there is no Rich Text Edit that can hook up to the sql components\Grid\dataset

I just use code to pass the RTF code from RichMemo to the DBMemo (and visa-versa) to be stored or edited in RichMemo.

That is working fine.

Q:
How can I duplicate a record that is visible in the grid so the user can just make a few edits to the copied record??

SCREENSHOT (NOTE: the RTF code DBMemo will not be visible to user - visible just for testing)


« Last Edit: April 26, 2019, 02:36:16 pm by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!


Pixelink Media •  Pixelink Forum
LAZ 2.02 • Win10 • 16G RAM • Nivida GForce RTX 2060

pixelink

  • Hero Member
  • *****
  • Posts: 943
    • Pixelink Media
Re: Displaying Data from DBGrid into DBFields - Error Hooking up fields
« Reply #18 on: April 26, 2019, 02:24:37 pm »
Another quick question

How do I capture if the user uses the UP/Down arrows to move advance records.

Preferably not using moue down or up key codes.
Preferably when the data-set has advanced records.

I need to code something in when they do.
« Last Edit: April 26, 2019, 02:35:13 pm by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!


Pixelink Media •  Pixelink Forum
LAZ 2.02 • Win10 • 16G RAM • Nivida GForce RTX 2060

mangakissa

  • Hero Member
  • *****
  • Posts: 921
Re: Displaying Data from DBGrid into DBFields - Error Hooking up fields
« Reply #19 on: April 26, 2019, 04:16:17 pm »
I'll give up
Lazarus 1.84 (32b) / FPC 3.0.4
Windows 10

pixelink

  • Hero Member
  • *****
  • Posts: 943
    • Pixelink Media
Re: Displaying Data from DBGrid into DBFields - Error Hooking up fields
« Reply #20 on: April 26, 2019, 05:01:19 pm »
The solution i found to duplicate a row is this ...

Because i could not find out how to extract any text from the DBGrid cells themselves (because the traditional row/cell properties are hidden from the control) I had to use a work around.

1) I decided to hook back up (and hide from user) 3 DB Memo Controls. Hooked them up to the sql components.

2) Add a button to my form and just simply get the text from the DB Memo controls, set them to variables, then insert a new record and send the var strings back to the DBMemo controls. Then POST (save) newly inserted record.

Works like a Charm!!!

Code: Pascal  [Select]
  1.  
  2. procedure TForm5.btnDuplicateClick(Sender: TObject);
  3. var
  4.   gTxt1: String;
  5.   gTxt2: String;
  6.   gTxt3: String;
  7.   gTxt4: String;
  8. begin
  9.  
  10. {get the currently selected row of text}
  11. gTxt1:=DBMemo1.Text;
  12. gTxt2:=DBMemo2.Text;
  13. gTxt3:=DBMemo3.Text;
  14. gTxt4:=DBMemo.Text;
  15.  
  16. {add a new blank record}
  17. DBGrid1.Datasource.dataset.insert;
  18.  
  19. {add duplicat vales}
  20. DBMemo1.Text:=gTxt1;
  21. DBMemo2.Text:=gTxt2;
  22. DBMemo3.Text:=gTxt3;
  23. DBMemo.Text:=gTxt4;
  24.  
  25. {save data}
  26. DBGrid1.Datasource.dataset.Post;
  27.  
  28. MessageDlg('Duplicate Record', 'A duplicate record has been made and selected. Press the "edit" button to edit the selected duplicate record.', mtInformation, [mbOK], 0);
  29. end;
  30.  
  31.  

May not be elegant or right, but it works
Can't Type - Forgetful - Had Stroke = Forgive this old man!


Pixelink Media •  Pixelink Forum
LAZ 2.02 • Win10 • 16G RAM • Nivida GForce RTX 2060

pixelink

  • Hero Member
  • *****
  • Posts: 943
    • Pixelink Media
Re: Displaying Data from DBGrid into DBFields - Error Hooking up fields
« Reply #21 on: April 26, 2019, 05:10:16 pm »
Just an FYI for those who may ask why I use DBMemo and not the DBEdit controls?

Because the DBEdit controls displays the actual word "MEMO" while a Memo text field doesn't.... it shows the correct text.

I don't know why the DBEdits work this way. There is no DisplayMemo (true/false) flag that displays the text and not "MEMO" like the DBGrid... it has this flag.

And I don't know why DBMemo shows regular text and not "MEMO".... you would think the later would be more appropriate.
Bug or missing feature in DBEdit... not sure.
« Last Edit: April 26, 2019, 05:18:10 pm by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!


Pixelink Media •  Pixelink Forum
LAZ 2.02 • Win10 • 16G RAM • Nivida GForce RTX 2060

lucamar

  • Hero Member
  • *****
  • Posts: 1829
Re: Displaying Data from DBGrid into DBFields - Error Hooking up fields
« Reply #22 on: April 26, 2019, 05:29:31 pm »
Just a question: instead of playing around with so many controls, why don't you deal directly with the DBGrid's underlying Datasource or that datasource's Dataset? It's them that do have the raw data the grid is using; easier to get it from the source, isn't it?

Just wondering ...
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4 & 2.0.2 w/FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

pixelink

  • Hero Member
  • *****
  • Posts: 943
    • Pixelink Media
Re: Displaying Data from DBGrid into DBFields - Error Hooking up fields
« Reply #23 on: April 26, 2019, 05:32:48 pm »
Just a question: instead of playing around with so many controls, why don't you deal directly with the DBGrid's underlying Datasource or that datasource's Dataset? It's them that do have the raw data the grid is using; easier to get it from the source, isn't it?

Just wondering ...

I would love to, but can't seem to find documentation on using all these things in a data-set.
I checked most of the wikis... and didn't seem to find them.

Do you know where it is at?
Can't Type - Forgetful - Had Stroke = Forgive this old man!


Pixelink Media •  Pixelink Forum
LAZ 2.02 • Win10 • 16G RAM • Nivida GForce RTX 2060

pixelink

  • Hero Member
  • *****
  • Posts: 943
    • Pixelink Media
Re: Displaying Data from DBGrid into DBFields - Error Hooking up fields
« Reply #24 on: April 26, 2019, 05:49:08 pm »
I have seen this page, but not much for code examples.
Some are easy to figure out others are not.

https://www.freepascal.org/docs-html/fcl/db/tdataset.html
Can't Type - Forgetful - Had Stroke = Forgive this old man!


Pixelink Media •  Pixelink Forum
LAZ 2.02 • Win10 • 16G RAM • Nivida GForce RTX 2060

pixelink

  • Hero Member
  • *****
  • Posts: 943
    • Pixelink Media
Re: Displaying Data from DBGrid into DBFields - Error Hooking up fields
« Reply #25 on: April 26, 2019, 06:04:08 pm »
Well... I was able to figure this out using just the dataset.
I spent hours trying to figure out how to get data from the grid. because that is what you would expect (like String/ListView)

THANK YOU LUCAMAR for pointing that out.

Code: Pascal  [Select]
  1. DBGrid1.DataSource.Dataset.FieldByName('field name').AsVariant
  2.  

HOWEVER, I don't see a property on that WIKI page to set the fields. with variables after I use Insert (blank fields)
This is where i get stuck.



All I see on wiki is SetDataField, but If I am interpeting it correctly. It not the same as puting the text in those fields, but taking existing text and setting them to a buffer.
« Last Edit: April 26, 2019, 06:20:46 pm by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!


Pixelink Media •  Pixelink Forum
LAZ 2.02 • Win10 • 16G RAM • Nivida GForce RTX 2060

lucamar

  • Hero Member
  • *****
  • Posts: 1829
Re: Displaying Data from DBGrid into DBFields - Error Hooking up fields
« Reply #26 on: April 26, 2019, 06:10:57 pm »
Wel, you could start at the Database Portal and keep going from there ... although it's true that the information in the wiki is rather basic or deals more with how to connect with different types of databases than with intermediate and advanced usage.

I remember having found some tutorials in the web but I don't remember where they were (a search should bring them up to light) and, besides, most of them were for Delphi. One needs to know quite a little bit to apply Delphi DB tutorials to FPC/Lazarus.

ETA: Oops! Our posts crossed :)

One thing:

Code: [Select]
DBGrid1.DataSource.Dataset.FieldByName('field name').AsVariant
Why use "AsVariant"?

If the field is stored as a string, use:
  FieldByName('field name').AsString
and if  it's in a BLOB, cast it to a TBlobField or even a TMemoField.

AsVariant should only be used when the field really contains a Variant.
« Last Edit: April 26, 2019, 06:26:11 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4 & 2.0.2 w/FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

pixelink

  • Hero Member
  • *****
  • Posts: 943
    • Pixelink Media
Re: Displaying Data from DBGrid into DBFields - Error Hooking up fields
« Reply #27 on: April 26, 2019, 06:25:33 pm »
One thing:

Code: [Select]
DBGrid1.DataSource.Dataset.FieldByName('field name').AsVariant
Why use "AsVariant"?

If the field is stored as a string, use FieldByName('field name').AsString and if  it's in a BLOB, cast it to a TBlobField or even a TMemoField.

AsVariant should only be used when the field really contains a Variant.

That is the code example provided on the WIKI.
However, I realize I can go and change all those field formats... like to AsString etc etc.
Can't Type - Forgetful - Had Stroke = Forgive this old man!


Pixelink Media •  Pixelink Forum
LAZ 2.02 • Win10 • 16G RAM • Nivida GForce RTX 2060

pixelink

  • Hero Member
  • *****
  • Posts: 943
    • Pixelink Media
Re: Displaying Data from DBGrid into DBFields - Error Hooking up fields
« Reply #28 on: April 26, 2019, 06:35:19 pm »
I did find this property.... but again... no code example.


Code: Pascal  [Select]
  1. TDataSet.SetFields

It usues 1 property... array of const

Code: Pascal  [Select]
  1. DBGrid1.DataSource.DataSet.SetFields(array of const):=gTxt1;

I think it needs the array number 0-3, but I get error ... saying needs array of consonant.

Obviously I am doing it wrong.


Lets see if I can search google a find some examples of use.
« Last Edit: April 26, 2019, 06:43:02 pm by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!


Pixelink Media •  Pixelink Forum
LAZ 2.02 • Win10 • 16G RAM • Nivida GForce RTX 2060

pixelink

  • Hero Member
  • *****
  • Posts: 943
    • Pixelink Media
Re: Displaying Data from DBGrid into DBFields - Error Hooking up fields
« Reply #29 on: April 26, 2019, 06:58:59 pm »
Okay... searching Google, found text example of putting text into a field of a data-set...
Works too....

Code: Pascal  [Select]
  1. Var
  2. gTxt: String;
  3.  
  4. begin
  5. gTxt1:=DBGrid1.DataSource.Dataset.FieldByName('Pub').AsString;  
  6. DBGrid1.Datasource.dataset.Last; {could use append too}
  7. DBGrid1.Datasource.dataset.insert;
  8. DBGrid1.DataSource.DataSet.Fields[0].AsString:=gTxt1;
  9. DBGrid1.Datasource.dataset.Post;
  10. end;
  11.  
  12.  

Funny... the "Fields" class is not on that WIKI page.

This IS WORKING!!! NO MORE USING THOSE DB Controls!!!  AWESOME!
« Last Edit: April 26, 2019, 07:02:36 pm by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!


Pixelink Media •  Pixelink Forum
LAZ 2.02 • Win10 • 16G RAM • Nivida GForce RTX 2060