重新画dbgrid,在onDrawDataCell事件中:
     if field.FieldName='c1_code' then
        field.DisplayText:=ComboDlg1.Text;
     为什么不能通过编译,我的本意是想重新画dbgrid,根据c1_code字段的代码值,动态画值,
     可是编译不过去,说"cannot assign to a read-only property";
     有没有人有过重新画dbgrid的,请帮帮忙呀。急,急,

解决方案 »

  1.   

    Field.displayText是只读的。你应该修改数据库记录的形式进行这项操作。
      

  2.   

    要在onDrawDataCell中重新画dbgrid,需用DrawText或TextOut函数。
    不过要实现你的功能最好在字段的OnSetText事件中实现。
      

  3.   

    这个是个在dbgrid里重新画个checkbox的例子
    procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    var
      can: TCanvas;
      bSel: Boolean;
    begin
       //
      can := TDBGrid(Sender).Canvas;
      bSel := false;
      if (gdSelected in state) then bSel := true;
      if (not Assigned(can)) then exit;
      case DataCol of
        0: DrawCheckBox(can, bSel, rect);
        //3: DrawPassword(can, bSel,rect);
        5: DrawModel(can, bSel,rect);
      end;
    end;procedure TForm1.DrawCheckBox(can: TCanvas; bSel: boolean; rect: TRect);
    const
      CHECKBOX_WIDTH = 12;
      CHECKBOX_HEIGHT = 12;
      RIGHT_WORD = '√';
    var
      boxRect, txtRect: TRect;
      bmp: TBitmap;
      n:integer;
    begin  can.Brush.Color := clWhite;
      can.Pen.Color := clWhite;
      if (bSel) then
      begin
        can.Brush.Color := GetSysColor(COLOR_HIGHLIGHT);
      end;
     // dec(r.Top);
      can.Rectangle(rect);
      //接下来画CHECKBOX
      boxRect.Left := (rect.Right + rect.Left - CHECKBOX_WIDTH) div 2;
      boxRect.Right := (rect.Right + rect.Left + CHECKBOX_WIDTH) div 2;
      boxRect.Top := (rect.Top + rect.Bottom - CHECKBOX_HEIGHT) div 2;
      boxRect.Bottom := (rect.Top + rect.Bottom + CHECKBOX_HEIGHT) div 2;
      //Draw3D(can, boxRect);
      //====
      bmp := TBitmap.Create;
      //====================================
      n:=strtoint(DBGrid1.Fields[0].AsString);
      try
        if (n>0) then
          ImageList1.GetBitmap(2,bmp)
        else
          ImageList1.GetBitmap(0,bmp);
          can.Draw(boxRect.left, boxRect.Top, bmp);
      finally
       bmp.Free;
      end;
      //===================================
    end;