Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Select a row in a DB Grid programmatically

Posted on 1998-12-18
6
Medium Priority
?
777 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 400 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Screencast - Getting to Know the Pipeline
Suggested Courses

783 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