RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TCustomDBGrid.SelectedIndex Property

Specifies the index of the currently selected column in the Columns array.

Pascal
property SelectedIndex: Integer;
C++
__property int SelectedIndex;

Set SelectedIndex to move focus to a column in the grid that is identified by position. Read SelectedIndex to determine which column in the grid has focus. A value of 0 indicates the first data column, 1 is the second data column, and so on. SelectedIndex is -1 if there is no currently selected column. 

If the Options property includes dgIndicator, the index of the data column given by SelectedIndex will differ from the index of the physical column in the grid. 

To access the field component for the selected column, use the SelectedField property.  

C++ Examples: 

 

/*
The following example enables the user to move the current
selected cell in a db grid.  The Up and Down buttons have
their OnClick events assigned to the UpDownClick procedure.
The Left and Right buttons have their OnClick events
assigned to the LeftRightClick procedure.  The Up and Left
buttons have their Tag property set to -1, while the Down
and Right buttons have their Tag property set to 1.  MoveBy
does not move the cell in the db grid, it only moves the
selection.  The data set is not edited.  Notice that the
Up button and the Down button are separate TButtons, not
one TUpDown.  The tags must be set different.
*/
void __fastcall TForm1::UpDownClick(TObject *Sender)
{
  CDS2->MoveBy(((TComponent *)Sender)->Tag);
  DBGrid2->SetFocus();
}

void __fastcall TForm1::LeftRightClick(TObject *Sender)
{
  DBGrid2->SelectedIndex = DBGrid2->SelectedIndex + ((TComponent *)Sender)->Tag;
  DBGrid2->SetFocus();
}

 

Delphi Examples: 

{
The following example enables the user to move the current
selected cell in a db grid.  The Up and Down buttons have
their OnClick events assigned to the UpDownClick procedure.
The Left and Right buttons have their OnClick events
assigned to the LeftRightClick procedure.  The Up and Left
buttons have their Tag property set to -1, while the Down
and Right buttons have their Tag property set to 1.  MoveBy
does not move the cell in the db grid, it only moves the
selection.  The data set is not edited.  Notice that the
Up button and the Down button are separate TButtons, not
one TUpDown.  The tags must be set different.
}
procedure TForm1.UpDownClick(Sender: TObject);
begin
  CDS.MoveBy(TComponent(Sender).Tag);
  DBGrid1.SetFocus;
end;

procedure TForm1.LeftRightClick(Sender: TObject);
begin
  DBGrid1.SelectedIndex := DBGrid1.SelectedIndex + TComponent(Sender).Tag;
  DBGrid1.SetFocus;
end;

 

Columns 

Options 

SelectedField 

Creating a Customized Grid

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!