0

I have this code to search through a DevExpress TcxGrid:

    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 > -1) and (CurIndex < 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;

Search works well as long as the grid is not sorted.

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?

Jan Doggen
  • 8,799
  • 13
  • 70
  • 144
Akella225
  • 61
  • 2
  • 8
  • Not necessarily an answer to your question but perhaps a way to leverage code Dev Express has already written, could you use the incremental search functionality that the grid provides? In the help file do a search on the "Incremental Search example". It shows you how to program this with only a few lines of code. – Sam M May 29 '13 at 16:47
  • Unfortunately GridView.DataController.Search searches only on the first character as IncSearch. But I need a search as POS(). – Akella225 May 30 '13 at 05:54
  • Yes now I see why need your own searching code. Can you describe what behavior you see when there is an incorrect position of the cursor after search (ie. where does the cursor end up)? – Sam M May 30 '13 at 17:35
  • The cursor focus to another record. – Akella225 May 31 '13 at 07:48
  • See the picture http://f3.s.qip.ru/14a5VJ5c5.png – Akella225 May 31 '13 at 07:59
  • I turned on the sort column, then I did a search of the text "548", but the cursor to hit another record – Akella225 May 31 '13 at 08:00
  • This user had the same problem I believe http://www.devexpress.com/Support/Center/Question/Details/Q398416 – Jan Doggen Jun 03 '13 at 12:14

0 Answers0