Ralf K
Ralf K

Reputation: 23

How do I list the bank's query on the same Memo line?

I have the following table:

tb_cars
------
BMW
VW
Chevrolet

The code I use is:

procedure Tfrm1.Button1Click(Sender: TObject);
var
 i: Integer;
begin
for i := 0 to DBGridCars.SelectedRows.Count - 1 do
begin
  DM.tbCars.Bookmark := DBGridCars.SelectedRows.Items[i];
  memoTags.Lines.Add((DM.tbCars.FieldByName('car').AsString) + ',' );
      end;
end;

If I select 2 cars in the DBGrid

The same is displayed:

VM,
Chevrolet,

There is a possibility to display:

VM, Chevrolet

On the same line without the last character ","?

Upvotes: 1

Views: 80

Answers (1)

Dale M
Dale M

Reputation: 843

Similar to what Tom stated in the comments, but you can use your loop variable count to know whether or not this is your first selection (and you only need the car name) or if it is an additional selection (and you need the comma before the car name).

procedure Tfrm1.Button1Click(Sender: TObject);
var
 i: Integer;
 s: string;
begin
  for i := 0 to DBGridCars.SelectedRows.Count - 1 do
  begin
    DM.tbCars.Bookmark := DBGridCars.SelectedRows.Items[i];

    if i = 0 then // first selection
      s := DM.tbCars.FieldByName('car').AsString
    else // additional selections
      s := s + ', ' + DM.tbCars.FieldByName('car').AsString;
  end;
    memoTags.Lines.Add(s);
end;

Upvotes: 1

Related Questions