Solved

Select a row in a DB Grid programmatically

Posted on 1998-12-18
6
752 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
Comment Utility
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
Comment Utility
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
Comment Utility
Please give more details, according to parameters you want to select a row?
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 4

Expert Comment

by:dwwang
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now