Delphi TDBGrid selected row get value

Nu Carvignulu picture Nu Carvignulu · Jan 21, 2017 · Viewed 32.8k times · Source

I would like to retrieve a value from a TDBGrid the selected row, how can I do?

procedure TForm7.Button2Click(Sender: TObject);
    var
      i, j: Integer;
      s: string;
    begin
      if DBGrid1.SelectedRows.Count>0 then
        with DBGrid1.DataSource.DataSet do
          for i:=0 to DBGrid1.SelectedRows.Count-1 do
          begin
            GotoBookmark(DBGrid1.SelectedRows.Items[i]);
            for j := 0 to FieldCount-1 do
            begin
              if (j>0) then s:=s+', ';
              s := s + FindField(Fields.Fields[j].FieldName).AsString;
            end;
            Listbox1.Items.Add(s);
            s:= '';
          end;
    end;

Answer

MartynA picture MartynA · Jan 21, 2017

The code below fixes a few problems with yours.

The main problems were the fact that you weren't correctly initialising s and your way of getting the fields for the selected row(s) was flawed.

The calls to DataSet.Disable/EnableControls and ListBox1.Items.BeginUpdate/EndUpdate are to speed the process up.

Also, avoid the with construct like the plague. As you can see, my use of a local DataSet variable instead involves minimal extra typing and avoids all sorts of accidental problems that can arise when you use with.

procedure TForm1.GetSelected;
var
  i,
  J : Integer;
  s : String;
  DataSet : TDataSet;
begin
  if DBGrid1.SelectedRows.Count>0 then begin
    DataSet := DBGrid1.DataSource.DataSet;
   //  with DBGrid1.DataSource.DataSet do
   try
     ListBox1.Items.BeginUpdate;
     DataSet.DisableControls;
     for i:=0 to DBGrid1.SelectedRows.Count-1 do
      begin
        DataSet.GotoBookmark(Pointer(DBGrid1.SelectedRows.Items[i]));
        s := '';
        for j := 0 to DataSet.FieldCount - 1 do
        begin
          if (j>0) then s:=s+', ';
          s := s + DataSet.Fields[j].AsString;
          //s := s + FindField(Fields.Fields[j].FieldName).AsString;
        end;
        Listbox1.Items.Add(s);
        //s:= '';
      end;
    finally
      DataSet.EnableControls;
      ListBox1.Items.EndUpdate;
    end;
  end;
end;

**Update: **

You can set the current grid row as selected like this

DBGrid1.SelectedRows.CurrentRowSelected := True;

Update #2

The grid's selected rows are stored in a TBookmarkList named SelectedRow. To clear the current selections, all you need do is to call its Clear method, like so:

procedure TForm1.btnClearSelectedClick(Sender: TObject);
begin
  DBGrid1.SelectedRows.Clear;
end;

Equally, if you want to clear your ListBox, you just call its Clear method, as in:

procedure TForm1.btnClearListBoxClick(Sender: TObject);
begin
  ListBox1.Clear;
end;

If you are having trouble getting my code to work, try this:

  1. In the Object Inspector, set the DBGrid Options property dgMultiSelect to True.

  2. Place a button on your form and in its OnClick handler call GetSelected.

Compile and run. Click a row in the grid and then the button. Nothing happens. The reason is that clicking the button moves focus away from the DBGrid, so that as far as it is concerned, none of its rows is selected. Then try step 3.

  1. Run the app again. This time press and hold the Ctrl key while you click the grid then the button. This time the selected row appears in the ListBox. With the Ctrl button still down, click another row in the grid, then the button. This time, both rows are added to the ListBox.