Board index » delphi » Multi-select in a DB grid

Multi-select in a DB grid

I'm using Delphi v.2, and have a TDBGrid on my form; the user can select
several records... how can I tell (in my program) which records are selected?  
Is there a "tagged" or "selected" property that is not documented?

After I solve that problem... if my program modifies each of the selected
records, is there an easy way to update the table such that all the selected
records are updated with one command?

Thanks,
Bob Hilton

 

Re:Multi-select in a DB grid


Quote
Bob Hilton wrote:

> I'm using Delphi v.2, and have a TDBGrid on my form; the user can select
> several records... how can I tell (in my program) which records are selected?
> Is there a "tagged" or "selected" property that is not documented?

> After I solve that problem... if my program modifies each of the selected
> records, is there an easy way to update the table such that all the selected
> records are updated with one command?

> Thanks,
> Bob Hilton

I had the same problem some time ago, but could not find any other
way around than creating an array of longint that stores the rowid
of the selected records. I the DBgrid's OnKeyPress or OnKeyDown you
can trap the keystroke you want for the selection. The 'rowid' must
be an unique field in the table i.e AutoIncrement (longint type) on
Paradox tables. (Other SQL tables may have direct rowid).
When the selection is done (and the array is filled), you can
write a loop that process the records  with the tables's locate
function.

Atle Markeng
DataMar. Oslo-Norway

Re:Multi-select in a DB grid


Hello,

follow these steps to create a sample project that shows you what
lines are selected in a DBGrid:

1) Create the following components in a form :
TTable, TDatasource, DBGrid, Panel, TButton

2) Set the properties of the components as follows:
TTable: Databasename = DBDEMOS; Tablename = COMPANY
TDatasource : Dataset = TABLE1
DBGrid: Datasource = DATASOURCE1; Options --> dgMultiSelect = TRUE
(Multiselect allows to select multiple rows in a DBGrid. It is
available only in Delphi 2.0!)

3) Write the following code for the OnClick event of the button:

procedure TForm1.Button1Click(Sender: TObject);
var I : integer;
    Book : TBookmark;
begin
   with table1 do begin
      DisableControls;        
      Book := GetBookmark;
      First;    
      while not EOF do begin
         if dbGrid1.SelectedRows.CurrentRowSelected then begin      
            showMessage(FieldByName('Company').AsString + ' is
selected');
         end;
         Next;
      end;
      GotoBookmark(Book);
      FreeBookmark(Book);
      EnableControls;
   end;
end;

Re:Multi-select in a DB grid


Quote
Bob Hilton wrote:
> I'm using Delphi v.2, and have a TDBGrid
> Is there a "tagged" or "selected" property that is not documented?

SelectedRows property which is not documented but can be used.

Quote
> After I solve that problem... if my program modifies each of the selected
> records, is there an easy way to update the table such that all the selected
> records are updated with one command?

Dataset.Refresh may do it but I'm not sure.
(Dataset is table, query or stored proc connected to your Datasource).

Re:Multi-select in a DB grid


Quote
>I had the same problem some time ago, but could not find any other
>way around than creating an array of longint that stores the rowid
>of the selected records. I the DBgrid's OnKeyPress or OnKeyDown you
>can trap the keystroke you want for the selection. The 'rowid' must
>be an unique field in the table i.e AutoIncrement (longint type) on
>Paradox tables. (Other SQL tables may have direct rowid).
>When the selection is done (and the array is filled), you can
>write a loop that process the records  with the tables's locate
>function.

This is a good idea but how do you mark the selected records so that
the user can see what records have been selected?

Regards, Mario Muja.

Other Threads