hello everybody


hello everybody, 

i tried to create new dbgrid component inherited from tdbgrid. my purpose is to easily display widememo or memo data type on it. i managed to display it, but there still problem when displaying the data, it's displayed double when the row get focus. just like  the preview. and here is the code i put ondrawcolumncell event.

i override the ancestor ondrawcolumcell event. 

procedure TWKDBGrid.DrawColumnCell(const Rect: TRect; DataCol: Integer;
  Column: TColumn; State: TGridDrawState);
var
  OutRect: TRect;
  F : TField;
  sTeks: string;
  lstate : TGridDrawState;
begin
  lstate := [gdSelected];
  F := Column.Field;

  if f <> nil then
  begin
    if (f.DataType =  ftWideString) or
       (f.DataType = ftWideMemo) or
       (f.DataType =  ftMemo) then
    begin
      if FShowMemoAsText then
      begin
        if gdSelected in lstate then
        begin
          if (f.DataType =  ftWideString) or
             (f.DataType = ftWideMemo) or
             (f.DataType =  ftMemo) then
          begin
             sTeks := f.AsString;
             Canvas.FillRect(Rect);
             Canvas.TextRect(Rect,Rect.Left,Rect.Top,sTeks);

          end;
        end;
         end else
      begin
        inherited DrawColumnCell(Rect, DataCol, Column, State);
      end;

    end else
    begin
      inherited DrawColumnCell(Rect, DataCol, Column, State);
    end;

  end;
end;

please can somebody guide me, where is the mistake in my code.

Comments

  1. well i change the code like this 

    if State = [] then
     begin
                   sTeks := f.AsString;
                         cannvas.TextRect(Rect,Rect.Left,Rect.Top,sTeks);
     end else
      begin
                 Canvas.Brush.Style := bsSolid;
                 sTeks := f.AsString;
          Canvas.TextRect(Rect,Rect.Left,Rect.Top,sTeks);
     end;

    but still lost the focus gradient color.... anything to improve in the code ?

    ReplyDelete
  2. procedure TYourClass.OnDrawCell(Canvas: TCanvas; aRow: Integer; Rect: TRect;
      State: TGridDrawState);
    var
      oldBrushStyle: TBrushStyle;
    begin
      with Canvas
      do begin

        if (State = []) // Not focused/selected/etc
        then begin
          oldBrushStyle := Brush.Style; 
          Brush.Style := bsSolid;
        end;

        TextRect(Rect, Rect.Left, Rect.Top + 1, f.AsString);
     
        if (State = [])
        then begin
          Brush.Style := oldBrushStyle;
        end;
      end;
    end;

    ReplyDelete
  3. alright lars...thank you very much for your help

    ReplyDelete

Post a Comment