Solved

Select a row in a DB Grid programmatically

Posted on 1998-12-18
6
759 Views
Last Modified: 2011-09-20
How would you select a row in a dbgrid programmatically? Is there a property or method to do this?
0
Comment
Question by:evansj
6 Comments
 
LVL 3

Expert Comment

by:KE
ID: 1351820
There's no straight way like "Row := 10";
You will have to locate the DataSet on the row you wish to select.
Try to be more specific, there may be a workaround.
0
 

Author Comment

by:evansj
ID: 1351821
What I'm looking for is a way to do the following:
for x: = 0 to dbgrid.rowcount - 1 do
begin
select the row, etc
end;
0
 
LVL 5

Expert Comment

by:ronit051397
ID: 1351822
Please give more details, according to parameters you want to select a row?
0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 4

Expert Comment

by:dwwang
ID: 1351823
The code below is actually equivalent to the code you need:

YourDataSet.First;
While not YourDataSet.EOF do
           YourDataSet.Next;

When every time the dataset's record is moved, the corresponding dbgrid row is selected.

You can set dgRowSelect and dgAlwaysShowSelection to True in the dbgrid's options, so you can see the selection at any time.

Regards,
Wang
     
0
 
LVL 5

Accepted Solution

by:
ronit051397 earned 100 total points
ID: 1351824
procedure TForm1.Button1Click(Sender: TObject);
var
  FBookmark: TBookmark;
begin
with DBgrid1 do
  if (dgMultiSelect in Options) and (DataSource.Dataset.Active) then
  begin
    with DataSource.Dataset do
    begin
      if (BOF and EOF) then Exit;
      DisableControls;
      try
        FBookmark := GetBookmark;
        try
          First;
          while not EOF do
          begin
            SelectedRows.CurrentRowSelected := True;
            Next;
          end;
        finally
          try
            GotoBookmark(FBookmark);
          except
          end;
          FreeBookmark(FBookmark);
        end;
      finally
        EnableControls;
      end;
    end;
  end;
end;

0
 

Author Comment

by:evansj
ID: 1351825
Excellent the code snippet works great. What I'm doing is copying the entire dbgrid to the clipboard for import into Excel. I can already copy the data to the clipboard if the user holds the ctrl key and selects the individual row. Thanks much!!!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi: how to send PJL commands to printer 3 100
Delphi application Soap connection 5 104
Mobile Keyboard covers the display of the TMemo 3 88
select query - oracle 16 100
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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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