Recent

Author Topic: Solved: TDBGrid keyUp,KeyDown event  (Read 2717 times)

xaver13

  • Full Member
  • ***
  • Posts: 111
Solved: TDBGrid keyUp,KeyDown event
« on: November 06, 2017, 11:01:18 am »
hello, I am using TdbGrid and I want to show some values from it  in another fields, when user clicks on it.

The solution is simple
I used a Query named Query1 and a Datasource and a DBGrid1CellClick event like this:

Code: Pascal  [Select][+][-]
  1. procedure TFormWallet.DBGrid1CellClick(Column: TColumn);
  2. begin
  3.      if query1.FieldByName('col_2').Value <> null then
  4.      edtCol_2.Text:=Query1.FieldByName('col_2').Value else edtCol_2.Text:='' ;
  5.      if query1.FieldByName('col_10').Value <> null then
  6.      edtCol_10.Text:=Query1.FieldByName('col_10').Value else edtCol_10.Text:='';
  7.      if query1.FieldByName('col_14').Value <> null then
  8.      edtCol_14.Text:=Query1.FieldByName('col_14').Value else edtCol_14.Text:='';
  9.  
  10. end;  
  11.  

It works fine, but the problem is, when I want to do the same when user uses keyboard, when I use KeyDownEvent simillar DBGridClick event like this
Code: Pascal  [Select][+][-]
  1. procedure TFormWallet.DBGrid1KeyDown(Sender: TObject; var Key: Word;
  2.   Shift: TShiftState);
  3. begin
  4.     //   query1.RecNo:=query1.RecNo+1 ;
  5.      if query1.FieldByName('col_2').Value <> null then
  6.      edtCol_2.Text:=Query1.FieldByName('col_2').Value else edtCol_2.Text:='' ;
  7.      if query1.FieldByName('col_10').Value <> null then
  8.      edtCol_10.Text:=Query1.FieldByName('col_10').Value else edtCol_10.Text:='';
  9.      if query1.FieldByName('col_14').Value <> null then
  10.      edtCol_14.Text:=Query1.FieldByName('col_14').Value else edtCol_14.Text:='';
  11.      // query1.RecNo:=query1.RecNo-1;
  12. end;
  13.  
record is the previous, and that is the problem,  I tried the solution with RecNo modification (commented here, but it is very stupid), even if I want to write KeyUpEvent too  ;)

PLease do you know better solution?

Thank you very much for help.
« Last Edit: November 06, 2017, 01:49:44 pm by xaver13 »
--
Jiri Cvrk

rvk

  • Hero Member
  • *****
  • Posts: 6169
Re: TDBGrid keyUp,KeyDown event
« Reply #1 on: November 06, 2017, 11:06:57 am »
This would depend on what key the user clicks.

But I don't think it's a good idea (and even what you want) to do it like you do.

Isn't it better to use TDBEdit instead of TEdit?
Of did you use TEdit (over TDBEdit) for a reason?

(With TDBEdit the edit-field will be automatically filled with your desired value from the database.)

PS. you can eliminate the if's if you do it like this:
Code: Pascal  [Select][+][-]
  1. edtCol_2.Text:=Query1.FieldByName('col_2').asString;
(asString will give an empty string if the field is Null

xaver13

  • Full Member
  • ***
  • Posts: 111
Re: TDBGrid keyUp,KeyDown event
« Reply #2 on: November 06, 2017, 01:49:26 pm »
Thank you very much for help! :)

I did not know about TDBedit.
--
Jiri Cvrk

 

TinyPortal © 2005-2018