title: Search in TcxGrid
body: <p>I have this code to search through a DevExpress TcxGrid:</p>
  
  <pre><code>    function SearchIncxGrid(AView: TcxGridTableView; AText: string; AFromBeginning: boolean): boolean;
    function Compare(const ARecIndex, AColIndex: integer): boolean;
    begin
      Result := AnsiContainsText(AView.DataController.DisplayTexts[ARecIndex, AView.VisibleColumns[AColIndex].Index], AText);
    end;
  
  var
    GroupsIndex: integer;
    GroupsCount: integer;
    ChildCount: integer;
    ColIndex: integer;
    RowIndex: integer;
    RecIndex: integer;
    CurIndex: integer;
    i, j, k: integer;
  begin
    Result := false;
    AView.DataController.ClearSelection;
  
    if AFromBeginning then
    begin
      // поиск с начала
      // строка  - первая
      // столбец - первый
      AView.DataController.GotoFirst;
      RowIndex := 0;
      ColIndex := 0;
    end
    else
    begin
      // поиск с текущей позиции
      // строка  - текущая
      // столбец - текущий
      // если текущий столбец - последний, то переходим к след. столбцу
      RowIndex := AView.Controller.FocusedRowIndex;
      ColIndex := AView.Controller.FocusedColumnIndex;
  
      if AView.Controller.FocusedColumn.IsLast then
      begin
        ColIndex := 0;
        Inc(RowIndex);
      end
      else
      begin
        Inc(ColIndex)
      end;
  
    end;
  
    if AView.DataController.Groups.GroupingItemCount = 0 then
    begin
      // поиск в несгруппированном представлении
      for i := RowIndex to AView.ViewData.RowCount - 1 do
      begin
        RecIndex := AView.ViewData.Rows[i].RecordIndex;
        if RecIndex = -1 then
          Continue;
  
        for j := ColIndex to AView.VisibleColumnCount - 1 do
        begin
          Result := Compare(RecIndex, j);
          if Result then
          begin
            AView.Controller.FocusedRecordIndex := RecIndex;
            AView.Controller.FocusedColumnIndex := j;
            Break;
          end;
        end;
  
        ColIndex := 0;
        if Result then
          Break;
      end;
    end
    else
    begin
      // поиск в сгруппированном представлении
      GroupsCount := TcxDataControllerGroupsProtected(AView.DataController.Groups).DataGroups.Count;
      GroupsIndex := AView.DataController.Groups.DataGroupIndexByRowIndex[RowIndex];
      for i := GroupsIndex to GroupsCount - 1 do
      begin
        ChildCount := AView.DataController.Groups.ChildCount[i];
        for j := 0 to ChildCount - 1 do
        begin
          RecIndex := AView.DataController.Groups.ChildRecordIndex[i, j];
          if RecIndex = -1 then
            Continue;
  
          CurIndex := AView.DataController.GetRowIndexByRecordIndex(RecIndex, false);
          if (CurIndex &gt; -1) and (CurIndex &lt; RowIndex) then
            Continue;
  
          for k := ColIndex to AView.VisibleColumnCount - 1 do
          begin
            Result := Compare(RecIndex, k);
            if Result then
            begin
              AView.Controller.FocusedRowIndex    := AView.DataController.GetRowIndexByRecordIndex(RecIndex, true);
              AView.Controller.FocusedColumnIndex := k;
              Break;
            end;
          end;
  
          ColIndex := 0;
          if Result then
            Break;
        end;
  
        if Result then  Break;
      end;
    end;
  
  //  if Result then
  //  begin
  //    AView.DataController.ClearSelection;
  //    AView.Controller.FocusedRecord.Selected := true;
  //  end;
  end;
  </code></pre>
  
  <p>Search works well as long as the grid is not sorted.</p>
  
  <p>When the cxGrid is sorted, there is an incorrect positioning of the cursor after search.
  Is there a universal and correct decision to search in cxGrid?</p>