delphi+DBgrid显示记录,如何在记录前显示一个小图标?就是每条记录前面显示一个固定的图片。记得以前找到过方法,忘记了。只记得用到了imagelist,dbgrid代码理加了imageindex什么的。

解决方案 »

  1.   

    试试这段
    procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
      Field: TField; State: TGridDrawState);
    var
      Bmp: TBitmap;
    begin
      if Field is TGraphicField then
      begin
        try
          Bmp:=TBitmap.Create;
          Bmp.Assign(Field);
          DBGrid1.Canvas.StretchDraw(Rect, Bmp);
        finally
          Bmp.Free;
        end
      end
      else
        DBGrid1.DefaultDrawDataCell(Rect, Field, State);
    end;
      

  2.   

    加IMAGELIST的话,试试下面这段
    procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject;
      const Rect: TRect; DataCol: Integer; Column: TColumn;
      State: TGridDrawState);
    var
      bitmap : TBitmap;
      fixRect : TRect;
      bmpWidth : integer;  imgIndex : integer;
    begin
      fixRect := Rect;  if DataCol = 1 then
      begin
        if ADOQuery1.Fields[1].Value > 10000 then
          imgIndex := 0 else imgIndex := 1;    bitmap := TBitmap.Create;
        try
          ImageList1.GetBitmap(imgIndex,bitmap);
          bmpWidth := (Rect.Bottom - Rect.Top);
          fixRect.Right := Rect.Left + bmpWidth;
          DBGrid1.Canvas.StretchDraw(fixRect,bitmap);
        finally
          bitmap.Free;
        end;    fixRect := Rect;
        fixRect.Left := fixRect.Left + bmpWidth;
      end;  DBGrid1.DefaultDrawColumnCell(fixRect, DataCol, Column, State);
    end;
      

  3.   

    樓上正解:
    1.可以在dbgrid的自繪事件中畫一個小圖標;
    2.直接拖一個iamgelist控件,在canvas中進行處理