AdvStringGrid.SelecetedRow[0]

Hello,

following code doesn't work as expected... gdSonglist is an advStringGrid.

While gdSongList.SelectedRowCount <> 0 do
Begin
  sel:=gdSongList.SelectedRow[0];
  gdSonglist.RowSelect[Sel]:=False;
  gdSonglist.RemoveRows(sel,1);
End;

The first iteration works fine (SelectedRow[0] returns 10), but in the second iteration SelectedRow[0] returns 11. It should return 10 again!?


Parts of the dfm source of the grid:

object gdSongList: TAdvStringGrid
      Left = 1
      Top = 29
      Width = 459
      Height = 323
      Cursor = crDefault
      Margins.Right = 1
      Align = alClient
      BorderStyle = bsNone
      Color = clWhite
      ColCount = 1
      Ctl3D = False
      DefaultColWidth = 90
      DrawingStyle = gdsGradient
      FixedColor = clSilver
      FixedCols = 0
      RowCount = 2
      Font.Charset = ANSI_CHARSET
      Font.Color = clBlack
      Font.Height = -13
      Font.Name = 'Segoe UI'
      Font.Style = []
      Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goColSizing, goColMoving, goRowSelect, goThumbTracking]
      ParentCtl3D = False
      ParentFont = False
      ParentShowHint = False
      PopupMenu = popMuSonglist
      ScrollBars = ssNone
      ShowHint = True
      TabOrder = 0
      DragScrollOptions.Delays.InitialDelay = 10
      EnableHTML = False
      EnhTextSize = True
      Look = glClassic
      MouseActions.DisjunctRowSelect = True
      MouseActions.NoAutoRangeScroll = True
      MouseActions.SelectOnRightClick = True
      MouseActions.SizeFixedCol = True
      MouseActions.WheelAction = waScroll
      Navigation.AutoGotoWhenSorted = True
      Navigation.AutoGotoIncremental = True
      Navigation.AllowClipboardAlways = True
      Navigation.KeepScrollOnSort = True
      Navigation.LeftRightRowSelect = False
      Navigation.KeepHorizScroll = True
      Navigation.HomeEndKey = heFirstLastRow
      ScrollType = ssMetro
      ScrollHints = shVertical
      SortSettings.AutoSortForGrouping = False
      SortSettings.DefaultFormat = ssAutomatic
      SortSettings.Column = 1
      SortSettings.Show = True
      SortSettings.IndexShow = True
      SortSettings.IndexColor = clLime
      SortSettings.AutoFormat = False
      SortSettings.IgnoreCase = True
      SortSettings.NormalCellsOnly = True
      Version = '7.4.0.2'
      WordWrap = False
      RowHeights = (22
        22)


Best regards
Sascha

There is no need to do a loop yourself, you can just call

grid.RemoveSelectedRows;

This is of course only a part of the code... Other things were done in the loop. I don't use the code for deselecting and removing only.
The user select the rows, choose a function from a popupmenu and then for each selected row other function calls will be done.

To loop through all selected rows, please use:


var
  i: integer;
begin
  for i := 0 to advstringgrid1.SelectedRowCount - 1 do
  begin
    listbox1.Items.Add(inttostr(advstringgrid1.SelectedRow));
  end;
end;

do NOT remove rows during this loop itself as it changes of course all indexes.
Remove the rows AFTER this loop with grid.RemoveSelectedRows;