3
votes

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;
3
What is your question? There is one obvious problem with the code in your q, namely that you do not explicitly initialise your variable s for each selected row: add s := ''; after the GotoBookmark... line. - MartynA

3 Answers

8
votes

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.
0
votes

only a single line call is needed to retrieve a value when you know the column number. The system will automatically return values from the current row, or last line clicked or double-clicked in a grid list.

var colidx : integer; ss : string;

..

ss := DBGrid1.Fields[colidx].AsString;
-1
votes

IF YOU'RE NEED TO FIND HOW TO GET DATA OF A SELECTED ROW IN THE DBGRID, I HAD THE HINT. I USE DBGRID.SelectedField TO GET THE SELECTED FIELD AND CONVERT THE FIELD TO BE SELECTED TO BOOKMARK BY DB_GRID.SelectedField.DataSet.Bookmark AND USE DATASET.GOTOBOOKMART TO GO TO THE SELECTED RECORD FIELD IN DBGRID DB_DATA.DataSet.GotoBookmark (DB_GRID.SelectedField.DataSet.Bookmark);