Jump to content
Lainkes

Check if selected row in DBGrid

Recommended Posts

Hello,

 

I guess it's an easy solution, but how do I check if a record is selected in a DBGrid?

If not selected, a button must be greyed out. If a record is selected, the button becomes active.

 

Thanks

 

Alain

Share this post


Link to post

I kind of don't understand your problem. What are you actually pursuing?
If you have only one record then the active buttons in the Navigator will be Insert/New, Edit and Delete. This is to say that you can't rely on the cursor buttons.
It is better to work with the attached DataSet. Either it has an active/selected record or it doesn't.
If you have something else in mind, then describe it more precisely.

Share this post


Link to post

Hello,

 

If a record (person) is selected in the DBGrid, there is a button that must be enabled..

The code behind the button links a new record to that perosn. So if no person is selected, the button must be disabled.

 

Alain

Share this post


Link to post

Perhaps I have a different concept of selected, but any non-empty dataset always has a current record, wich is where the indicator is when dgIndicator is part of the options. The field values from the dataset resemble this current record/row.

 

In addition to this current row, a TDBGrid also has some SelectedRows represented by a TBookmarkList.

 

So with

45 minutes ago, Lainkes said:

If a record (person) is selected in the DBGrid

do you mean the current row or the bookmark list?

Share this post


Link to post
Quote

any non-empty dataset always has a current record,

Indeed. I did not notice that. Thanks for the tip.

So I don't need to check.

 

Share this post


Link to post

It seems to me that you are referring to the Master - detail link. That is handled differently.
If it is something else so:

  1. the button bothers me a lot
  2. I would run the code every time I change a record
  3. but the code is time consuming...

Which of these applies?

Share this post


Link to post

@Lainkes - this is probably what you are after. 

 

Put the if/then code snippet (below) into the OnColEnter event of the DBGrid.  And every time you enter that field that you set .FieldName='myfieldname' to, will highlight your button bold or non-bold. 

procedure TForm1.dbgrid1ColEnter(Sender: TObject);
begin
  if db1.SelectedField.FieldName='myfieldname' then
    button1.Font.Style := [fsBold] else button1.Font.Style := [];
end;

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×