1

I have a TDBGRID called Grid in my application with 3 columns:

EMP_ID    EMP_FirstName    EMP_LastName

My grid is multiselect.

How can I get the EMP_ID of the selected rows when the user clicks a button (I want to pass them to a stored procedure).

Jan Doggen
  • 8,799
  • 13
  • 70
  • 144

1 Answers1

1

If I understand what you would like, the code below should help.

The rows in a DBGrid which are selected are recorded in bookmarks stored in its SelectedRows property. So, to get a list of the procedure TForm1.GetBookmarkIDs, what you need to do is:

  • Save your current position in the grid (CDS1.GetBookmark)

  • Iterate the SelectedRows' items to get each bookmark in turn, get the bookmark, get the dataset to go to that bookmark and get the EMP_ID for the dataset row (in my test data, the column name happens to be just 'ID').

  • Do whatever you like with the EMP_ID value

  • Return to the record we bookmarked at the start

The calls to DisableControls and EnableControls are to speed things up as they prevent other data-aware controls in the GUI (as well as the grid, of course) from being updated while the selected rows are visited.

Code:

procedure TForm1.GetBookmarkIDs;
var
  BM,
  SelectedBM : TBookmark;
  EMP_ID : Integer;
  i : Integer;
begin
   BM := CDS1.GetBookmark;
   try
     CDS1.DisableControls;
     CDS1.First;
     for i  := 0 to DBGrid1.SelectedRows.Count - 1 do begin
       SelectedBM := PChar(DBGrid1.SelectedRows[i]);
       CDS1.GotoBookmark(SelectedBM);
       EMP_ID := CDS1.FieldByName('ID').AsInteger;
       Memo1.Lines.Add(IntToStr(EMP_ID));
     end;
   finally
     CDS1.GotoBookmark(BM);
     CDS1.FreeBookmark(BM);
     CDS1.EnableControls;
   end;
 end;

You didn't say exactly how you need to format your list of EMP_IDs to send to the stored procedure. With the above code, you would do something like call the Stored Proc each time around the "for i := 0 ..." loop.

If you wanted them, say, as a comma-delimited list, you could rewrite the routine as a function returning a string, along these lines

function TForm1.GetBookmarkIDs : String;
[...]
begin
  Result := '';
  [...]
       //EMP_ID := CDS1.FieldByName('ID').AsInteger;
       if Result <> '' then
         Result := Result + ', ';
       Result := Result + CDS1.FieldByName('ID').AsString;
       [...]
MartynA
  • 30,454
  • 4
  • 32
  • 73
  • The description is ok but the code is somehow crude. I would have suggest a method for common use like `procedure GetSeletedFieldValueFromGrid(Grid:TDBGrid; const Fieldname:string; out SelectedValues: TVariantDynArray);`. You can get everything you need from the grid itself. – Sir Rufo Nov 14 '15 at 08:27
  • @SirRufo: Yes, but the OP sounds like a novice (" I code With Delphi 7, For educational Reasons") , so I thought it more important to concentrate on the basic details of how to get the EMP_IDs from the rows rather than the grid (what if the selected rows are only the top and bottom rows of a 30000-row grid?). In any case, I don't recall that D7 supports TVariantDynArray. Crude it may be, but I think that's better that distracting the OP with niceties. Op could write a more elegant version once learned a bit more. – MartynA Nov 14 '15 at 08:37
  • I would say, that you can teach a novice also the relations between the Grid, DataSource, DataSet with such a common method. And `TVariantDynArray` is just a single line of type declaration. – Sir Rufo Nov 14 '15 at 09:03
  • Did this answer your question? – MartynA Nov 17 '15 at 20:34
  • this line: SelectedBM := PChar(DBGrid1.SelectedRows[i]); gives me Incompatible types Error.. because first is Tarray and second is PWideChar – yesIamFaded May 29 '20 at 10:50
  • works when I dont go with PChar() I am taking the name field in my application and try to show it with ShowMessage but it doesnt fire – yesIamFaded May 29 '20 at 10:56
  • Sorry, it's waste of time trying to discuss this in comments. If you're having a problem, ask a new SO question and make sure you include a [minmal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) and details like Delphi version. – MartynA May 29 '20 at 11:16