Board index » delphi » DBGrid and controlling field focus

DBGrid and controlling field focus

How can I set the focus of a given a field (column) in a DBGrid component.

For example : if i click on a button, i wish to set the focus to the first
column of the DBGrid.

Danny Wijnschenk
da...@pophost.eunet.be

 

Re:DBGrid and controlling field focus


I had a similar problem...but the fix is easy.  You'll need to
add just a couple lines of code to your button:

   TheGrid.SetFocus;
   TheGrid.SelectedIndex := 4;

The selected index refers to the number in the index property
in the fields editor of the field you would like to set focus
to.

Hope this helps,

-Mike

Re:DBGrid and controlling field focus


Quote
On Thu, 08 Feb 96 19:28:57 PDT, op...@pophost.eunet.be wrote:
>How can I set the focus of a given a field (column) in a DBGrid component.

>For example : if i click on a button, i wish to set the focus to the first
>column of the DBGrid.

You can move the focus in a TDBGrid to a specific column in two easy ways.
The first is by a column's relative position in the grid, using the
SelectedIndex property. The relative position number for columns starts at
zero. So to set focus to the second column:

  procedure TForm1.Button1Click(Sender: TObject);
  begin
    DBGrid1.SelectedIndex := 1;
    DBGrid1.SetFocus;
  end;

The second means is by a reference to the field as a TField, allowing you
to set focus to a column based on the name of the field, using the
SelectedField property.

  procedure TForm1.Button2Click(Sender: TObject);
  begin
    DBGrid1.SelectedField := Table1.FieldByName('ADDRESS');
    DBGrid1.SetFocus;
  end;

**************************************************************************
Steve Koterski                    "Knowledge advances by steps, and not by
Product Group Manager             leaps."
Delphi Technical Support                       -- Lord Macaulay, 1800-1859
Borland International, Inc.

Re:DBGrid and controlling field focus


Quote
op...@pophost.eunet.be wrote:

> How can I set the focus of a given a field (column) in a DBGrid component.

> For example : if i click on a button, i wish to set the focus to the first
> column of the DBGrid.

> Danny Wijnschenk
> da...@pophost.eunet.be

Try setting the value of the SelectedIndex propertry of the DBGrid to
the index of the field in the underlying dataset.

HTH

--
Russell Arnold
Objective Software
Gidgegannup, Western Australia
objec...@iinet.net.au

Re:DBGrid and controlling field focus


Quote
On Thu, 08 Feb 96 19:28:57 PDT, op...@pophost.eunet.be wrote:

>How can I set the focus of a given a field (column) in a DBGrid component.

>For example : if i click on a button, i wish to set the focus to the first
>column of the DBGrid.

Use SelectedIndex to choose the column :-

        DBGrid.SelectedIndex := 0;              { 1st column in DBGrid }

The current row depends on the current record in the database, so you'll need to
do a findkey or similar on the dataset the grid is associated with.

Hope that helps,

Simon.

Re:DBGrid and controlling field focus


In <311D4388.7...@iinet.net.au> Russell Arnold <objec...@iinet.net.au>
writes:

Quote

>op...@pophost.eunet.be wrote:

>> How can I set the focus of a given a field (column) in a DBGrid
component.

>> For example : if i click on a button, i wish to set the focus to the
first
>> column of the DBGrid.

>> Danny Wijnschenk
>> da...@pophost.eunet.be

>Try setting the value of the SelectedIndex propertry of the DBGrid to
>the index of the field in the underlying dataset.

>HTH

>--
>Russell Arnold
>Objective Software
>Gidgegannup, Western Australia
>objec...@iinet.net.au

Here is the correct code for this:

  LinGrid.SelectedField := LinTbl.fieldByName('QNSQty');
  LinGrid.Setfocus;

Ben
Arrow

Other Threads