Solved

Select a row in a DB Grid programmatically

Posted on 1998-12-18
6
760 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

821 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