DBGrid multiselect

Can someone give me example how to use DBGrid.SelectedRows property?
I need ability
1. Select/unselect row in DBGrid
2. Check if specific row selected
3. Get all selected rows.

RowSelect and MultiSelect property of DBGrid set to True.
LVL 3
SlavakAsked:
Who is Participating?
 
kretzschmarConnect With a Mentor Commented:
hi slavak,

1.+2.

procedure TForm1.Button3Click(Sender: TObject);
begin
  if dbgrid1.SelectedRows.CurrentRowSelected then  //check
  begin
    ShowMessage('This Record was selected');
    dbgrid1.SelectedRows.CurrentRowSelected := False; //Unselect
  end
  else
  begin
    ShowMessage('This Record was not selected');
    dbgrid1.SelectedRows.CurrentRowSelected := True;//select
  end;
end;

3.

procedure TForm1.Button4Click(Sender: TObject);
var I : Integer;
begin
  for i := 0 to dbgrid1.SelectedRows.Count - 1 do
  begin
    dbgrid1.DataSource.Dataset.GotoBookmark(TBookmark(dbgrid1.SelectedRows[i]));
    //Do Something
    ShowMessage(dbgrid1.DataSource.Dataset.Fields[0].AsString);
  end;
end;

meikl
0
 
RBertoraCommented:
Hi, the following code is not spoon feeding..,
but you should have enough to figure out what you need
Rob ;-)

(*
I need ability
1. Select/unselect row in DBGrid
set dbgrid options, multiselect = true, rowselect = true


2. Check if specific row selected
3. Get all selected rows. *)

procedure TForm1.Button3Click(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(pointer(DBGrid1.SelectedRows.Items[i]));
        for j := 0 to FieldCount-1 do
        begin
          if (j>0) then s:=s+', ';
          s:=s+Fields[j].AsString;
        end;
        memo1.Lines.add(S);
        s:='';
      end;
end;
0
 
kretzschmarCommented:
oops, sorry rob,
didn't see your comment
meikl ;-)
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
mhervaisCommented:
Rob I really think it ia a tricky solution to do it. Unfortunately there is no other one.

And what is most tricky is that you have to set multiselect to true even if you want to make single selections. because even if the doc says that it works with multiselect = false, it does not.

I lost a huge time on this one :=(

Marc
0
 
RBertoraCommented:
No worries Meikl :-)
Hope slavak is happy with good solution you provided.
Rob ;-)
0
 
SlavakAuthor Commented:
Thanks, guys.

But may be someone know another way to do the same.

Problem with this way is that I need to reposition database cursor for every operation. This make some unnecessary visual effects on dbgrid (sure I can use LocateWindowUpdate function and return pointer) and need a "long" time.
0
 
kretzschmarCommented:
? slavak,

yes, of course,
u must use the database-cursor to check, select or unselect a record,
also if you walk through the bookmarks, because you must do the operation with the current-record.

to avoid the side-effects you can also use the disablecontrols/enablecontrols-methods of the underlying dataset.

meikl ;-)
0
 
MoondancerCommented:
This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was "stuck" against userID -1 versus the intended expert whom you awarded.  This corrects the problem and the expert will now receive these points; points verified.

Please click on your Member Profile and select "View Question History" to navigate through any open or locked questions you may have to update and finalize them.  If you are an EE Pro user, you can also choose Power Search to find all your open questions.

This is the Community Support link, if help is needed, along with the link to All Topics which reflects many TAs recently added.

http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
http://www.experts-exchange.com/jsp/zonesAll.jsp
 
Thank you,
Moondancer
Moderator @ Experts Exchange
0
All Courses

From novice to tech pro — start learning today.