2006. június 27., kedd
Using two Data Controls to display Data Source and Table data
Problem/Question/Abstract:
How can I use two different data controls to display data from a single data source and table?For instance, I'd like to use a DBGrid as a navigating tool as follows:
Clients table with standard name, phone number etc.
One DBText for each edit-able field
DBGrid contains just the name of the client
Click on a record in the DBGrid and the DBTexts are updated according to the record clicked.
At the moment, I'm using two data sources because I don't want phone numbers, etc. to show up in the DBGrid. However, because they're separate data sources, when I click on the DBGrid the DBTexts are not updated. Is there any way I can do this? The clients table is going to be hundreds of entries long, and paging through them is a pain. Also, editing them all in a grid would result in a grid about 25 columns wide :-( and this is also a pain.
Answer:
The Delphi 1.0 Method
If you're using Delphi 1.0, you can't do it with native Data Control components. They just don't have the capability. However, you can simulate the behavior of TDBGrid with a TListBox (this is just one of number of ways to approach this problem). At program startup, you would load the values from your Client Name field into the TListBox by doing the following (assuming you have a TTable and TListBox embedded on your form):
Table1.Open;
with Table1 do {Load the list box}
while not EOF do
begin
ListBox1.Items.Add(FieldByName('Client Name').AsString);
Next;
end;
ListBox1.ItemIndex := 0; {Select the first item in the list}
What you have to do next is trap the OnClick event of your TListBox so that whenever a user moves to or clicks on a new item, the value of that item is used to search the TTable and synchronize the values of your TDBEdit components. Assuming that you have your table keyed with the Client Name, you can do the following in the OnClick event;
procedure TForm1.ListBox1Click(Sender: TObject);
begin
with Table1 do
FindKey([ListBox1.Items[ListBox1.ItemIndex]]);
end;
You could quit here, but your work isn't quite done yet. You will have to be able to handle deletes and inserts to the table, and with those actions you need to update the list as well. I won't go into real specifics here for brevity's sake, but I'll give you an idea of what you have to do below:
Handling Inserts
In this situation you'll probably be better off using another form that has only TEdits that correspond to the fields in your table that you want to update, and OK and Cancel buttons on it. When the user clicks OK, you'll do a Form1.Table1.Insert; to insert a new record into the database and use the TTable.FieldByName method to update the fields.
Once you're done doing that, you have to add the new client name to the list. Since you want to simulate the indexing of your table in your list box, you can't just arbitrarily add an item. You have to load the entire list over again, using the code in the FormCreate method to accomplish this. However, you can get a HUGE performance gain by using the WinAPI function LockWindowUpdate to prevent the form from continuously painting while the list gets loaded. Here's an example of the modified loading code:
LockWindowUpdate(Form1.Handle);
while not EOF do
begin
ListBox1.Items.Add(FieldByName('Prompt').AsString);
Next;
end;
LockWindowUpdate(0);
Now the list will load, and the user won't even see the update happening. I'll leave it up to you to write the code to get to the newly added item.
Handling a Delete
Handling a delete is a simpler matter because it doesn't involve another form. All you do is call the TTable.Delete method to delete the current record. After that, reload the list box using the technique described above.
The Delphi 2.0 Method
Fortunately, with the arrival of Delphi 2.0, all the stuff we had to do above is a wash -- for the simple reason that the TDBGrid has more features. The one in particular that is relevant to this topic is the Columns Editor. With the TDBGrid's Columns Editor, you can specify which columns to display in the grid and how to display them. Now you don't have to rely on adding TTable TFields to the form, which severly limited what kinds of objects you could use to access the other field values if you changed the display properties. Now, you can drop a grid on your form, attach it to your table, and pick which column you want to display. Then you can drop other Data Controls such as TDBEdits and such and point them at the same table.They share the same data source, so any change such as movement or editing within the TDBGrid will be immediately reflected in the other Data Controls.
Feliratkozás:
Megjegyzések küldése (Atom)
Nincsenek megjegyzések:
Megjegyzés küldése