Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

DBGrid multiselect

Posted on 1999-10-11
8
Medium Priority
?
4,081 Views
Last Modified: 2008-02-20
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.
0
Comment
Question by:Slavak
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 7

Expert Comment

by:RBertora
ID: 2116287
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
 
LVL 27

Accepted Solution

by:
kretzschmar earned 200 total points
ID: 2116359
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2116367
oops, sorry rob,
didn't see your comment
meikl ;-)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:mhervais
ID: 2116381
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
 
LVL 7

Expert Comment

by:RBertora
ID: 2116546
No worries Meikl :-)
Hope slavak is happy with good solution you provided.
Rob ;-)
0
 
LVL 3

Author Comment

by:Slavak
ID: 2118902
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2120397
? 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
 
LVL 1

Expert Comment

by:Moondancer
ID: 6852906
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question