Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 06-26-2001, 01:18 AM
JJe JJe is offline
Senior Member
 
Join Date: May 2001
Posts: 120
Default How to get field values from Table to EditBox?

Hello.
I have DBGrid and EditBox components. I wan't to get field values from Table1 to Edit1, Edit2 and Edit3 when I double click field in DBGrid. Code look something like that:


procedure TForm1.DBGrid1DblClick(Sender: TObject);
begin
DataModule1.Query1.Close;
DataModule1.Query1.SQL.Clear;
DataModule1.Query1.SQL.Add( 'Select Field1, Field2, Field3 From dbo.Table1' );
DataModule1.Query1.SQL.Add( 'Where Field1 = '+DBGrid.SelectedField.AsString+'' );
DataModule1.Query1.Open;
Edit1.Text := DataModule1.Query1.FieldByName(Field1).asString;
Edit2.Text := DataModule1.Query1.FieldByName(Field2).asString;
Edit3.Text := DataModule1.Query1.FieldByName(Field3).asString;
DataModule1.Query1.Close;
end;


The code works if there is unique value in Field1, like
Field1
2000
2010
2020

So, now the problem is that if values in Field1 is same, like
Field1
2000
2000
2000

then the values I get to EditBoxes is always the first row values of the table. How to get values of the selected row of table? For example if I double click last field of table, how to get values of that row.

Sorry, its a quite hard to explain that but I hope that someone understood my problem...maybe LörAn can help me again...




- JJe
Reply With Quote
  #2  
Old 06-26-2001, 01:38 AM
LörAn LörAn is offline
Senior Member
 
Join Date: Mar 2001
Posts: 1,506
Default RE: How to get field values from Table to EditBox?

When you open a TQuery, your cursor is positionned on the first record. So that's why you display the first record values.
Now for your problem, I think the simplest is to use a DataSource. Then you have lots of possible solutions :

1- You can replace your Edits by DBEdits linked to the same datasource as your DBGrid and setting the DataField property to the correct ones. They'll be updated automatically each time you move your cursor onto the DBGrid. Then nothing else to do, but you have to take care, because if you allow modifications they'll be applied directly on the table linked to the DBGrid (sometimes we prefer having the ability to cancel updates...).

2- Put an invisible DBEdit (property visible set to false) linked to the same DataSource as your DBGrid and to the KeyField (ie. moreoften the ID field). On its "OnChange" event you'll just have to update the Edits with the current values, eg :

procedure DBEIDChange(Sender: TObject);
begin
Edit1.Text := DataModule1.Query1.FieldByName('Field1').asString;
Edit2.Text := DataModule1.Query1.FieldByName('Field2').asString;
[...]
end;

Many other solutions (like working directly with TQuery or TTable events), but I think these 2 ones are simple to understand.

Hope it helps U. Don't hesitate to ask for more explanations...

LörAn.
Reply With Quote
  #3  
Old 06-26-2001, 02:07 AM
ujsoft ujsoft is offline
Member
 
Join Date: Dec 2000
Posts: 34
Default RE: How to get field values from Table to EditBox?

If you sure that Query1 returns correct count of rows
use Query1.Next 1 time to get second row, 2 times to
get third row ...

In general something wrong in structure of your database.
The better idea to use DBGrid for Query1.

-ujsoft-
ujsoft.itgo.com
Reply With Quote
  #4  
Old 06-26-2001, 04:15 AM
JJe JJe is offline
Senior Member
 
Join Date: May 2001
Posts: 120
Default RE: How to get field values from Table to EditBox?

Thanks you for answering. Problem seems to be that how to get cursor position point to the record I have clicked. And is that possible to do. Any ideas??

- JJe
Reply With Quote
  #5  
Old 06-26-2001, 04:20 AM
LörAn LörAn is offline
Senior Member
 
Join Date: Mar 2001
Posts: 1,506
Default RE: RE: How to get field values from Table to EditBox?

When you click on the TDBGrid, your cursor's position is automatically updated, ie. you don't have to do anything.
But beware, when you call code like Query1.First, or Query1.Refresh or even Query1.Close;Query1.Open, then the cursor position is set to the first record...

LörAn.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 11:23 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.