2016-08-10 2 views
3

쿼리를 수행하고 반환 된 데이터를 dbgrid에 표시하고 있습니다.DBGrid의 특정 텍스트 강조 표시

검색 기준과 일치하는 항목을 강조하고 싶습니다. 같은 뭔가 :

검색 기준 : "테스트"DBGrid를에서

는, 데이터가 될 것이다 돌려 보냈다.

ID  Return 
1  This is a **test** 
2  **Test**ing 

여기서의 목표는 데이터 쿼리에 대한 의문입니다. 그러나 DBGrid에서 특정 텍스트를 강조하는 방법?

중요 : 텍스트의 특정 부분 만 강조 표시해야합니다.

참고 : 제시된 정보는 현실과 정확히 일치하지는 않습니다.

+0

. 그리고 다른 구성 요소에 대해 묻는 질문은 [도움말/주제에 따라] 주제와 관련이 없습니다. 우리는 컴포넌트 쇼핑 네트워크가 아닙니다. –

+2

이론 상으로는 Default Drawing을 false로 설정하고 OnDrawColumnCell 및/또는 OnDrawDataCell 이벤트를 사용하여 셀을 직접 그릴 수 있습니다.하지만 이는 꽤 낮은 수준에서 상당한 양의 작업입니다. 그리드 자체로는 할 수 없습니다. – Dsm

+0

DevExpress gridview가 바로이 작업을 수행하지만 저렴하지는 않습니다. – GuidoG

답변

6

DBGrid를

에서이 절차의 하이라이트`FilterText은 '
procedure HighlightCellText(AGrid :TDbGrid; const ARect : TRect; AColumn : TColumn; FilterText : string; AState:TGridDrawState ; 
    BkColor : TColor = clYellow; SelectedBkColor : TColor = clGray); 
var 
    HlRect : TRect; 
    Position : Integer; 
    HlText, FilterColName,DisplayText: string; 
    i, offset : Integer; 
begin 
    DisplayText := Acolumn.Field.AsString; 
    Position := Pos(AnsiLowerCase(FilterText), AnsiLowerCase(DisplayText){ AnsiLowerCase(AColumn.DisplayText)}); 
    if Position > 0 then 
    begin 
    // set highlight area 
    case AColumn.Alignment of 
     taLeftJustify: HlRect.Left := ARect.Left + AGrid.Canvas.TextWidth(Copy(DisplayText, 1, Position-1)) + 1; 
     taRightJustify: begin 
     Offset := AGrid.Canvas.TextWidth(Copy(DisplayText, 1,1)) - 1; 
     HlRect.Left := (ARect.Right - AGrid.Canvas.TextWidth(DisplayText)-offset) + AGrid.Canvas.TextWidth(Copy(DisplayText, 1, Position-1)); 
     end; 
     taCenter: begin 
     Offset := ((ARect.Right - ARect.Left) div 2) - (AGrid.Canvas.TextWidth(DisplayText) div 2) - (AGrid.Canvas.TextWidth(Copy(DisplayText, 1,1)) - 2); 

     HlRect.Left := (ARect.Right - AGrid.Canvas.TextWidth(DisplayText)- offset) + AGrid.Canvas.TextWidth(Copy(DisplayText, 1, Position-1)); 
     end; 
    end; 

    HlRect.Top := ARect.Top + 1; 
    HlRect.Right := HlRect.Left +AGrid.Canvas.TextWidth(Copy(DisplayText, Position, Length(FilterText))) + 1 ; 
    HlRect.Bottom := ARect.Bottom - 1; 

    //check for limit of the cell 
    if HlRect.Right > ARect.Right then 
     HlRect.Right := ARect.Right; 

    // setup the color and draw the rectangle in a width of the matching text 
    if gdSelected in AState then 
     AGrid.Canvas.Brush.Color := SelectedBkColor 
    else 
     AGrid.Canvas.Brush.Color := BkColor; 

    AGrid.Canvas.FillRect(HlRect); 

    HlText := Copy(DisplayText,Position, Length(FilterText)); 
    AGrid.Canvas.TextRect(HlRect,HlRect.Left + 1,HlRect.Top + 1, HlText); 
    end; 
end; 

는 DbGrid.OnDrawColumnCell 이벤트를 사용

예를 들어 강조 텍스트는 "RO"입니다.

procedure TForm6.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; 
    DataCol: Integer; Column: TColumn; State: TGridDrawState); 
begin 
    HighlightCellText(TDBGrid(Sender),Rect, Column,'ro',State); 
end; 

결과 :

enter image description here

편집 : 표준 DBGrid를에 불가능

A litle demo

+1

사용자가 편집 컨트롤에서 검색 문구를 입력하고 눈금이 눈금을 강조 표시하는 데 사용하는 데모를 제공 할 수 있습니까? 내가 볼 수있는 유일한 방법은 전체 그리드를 무효화하는 것인데, 이는 추악한 깜박임을 유발할 것입니다. –

+0

Val Marinov 정말 고마워요! 주어진 방법은 아무 문제없이 완벽하게 작동합니다. – Marcoscdoni

+0

@KenWhite Ok. 여기에 데모가 있습니다. 충분한가? https://www.youtube.com/watch?v=pGKSDT5eSPA&feature=youtu.be –