Using commands concerning DBGrid from the main form in another form
I'm making a simple program for use in auto开发者_StackOverflow社区motive parts shop. Here's how it's supposed to look: Link
The problem is the small window on the left. It should be opened when double clicked on any of the rows in DBGrid in the main window, and it should show all of the selected item's characteristics in DBEdit fields. If the Save button is clicked it should save the changes from the DBEdit fields into the database, but otherwise it should just ignore the changes.
I succeeded opening another form by double click on a field in DBGrid by using this code:
procedure TForm1.DBGrid1DblClick(Sender: TObject);
begin
if not Assigned(Form2)
then Form2 := TForm2.Create(Application);
Form2.Show;
end;
The only problem now is how to get the program to detect which row in the DBGrid is selected and then show its contents in DBEdit fields in the smaller window.
Can anyone tell me how to do this, please?
Thanks!
Just add the TDBEdits
to TForm2
, and connect them to the same DataSource
as the DBGrid
is using. They will automatically show the contents of the same row that is selected in the DBGrid
, and you can edit or insert into the DataSource's
DataSet
and have the new or changed data appear in the DBGrid
automatically.
There are many ways to achieve this. I'll describe two:
- If you link the DBEdits in the little window to the same DataSource as the DBGrid's, then you are all set
OR
- You can pass whatever info you want inside
DBGrid1DblClick
from Form1 to Form2. This option, by itself, has many possibilities.
Update
For the DataSource to be visible in Form2, unit to make Form2's unit use Form1's unit.
精彩评论