Solved

Select a row in a DB Grid programmatically

Posted on 1998-12-18
6
767 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
[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
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
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: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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month5 days, 5 hours left to enroll

636 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