我想实现在DBGrid中单击某个单元格时,然后把ComboBox定位到这个单元格的位置,请问如何实现

解决方案 »

  1.   

    参照:
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    在dbgrid的onDrawColumnCell事件里面处理 如下:
    我处理的是显示金额的一个控件,你修改一下吧
    procedure Tfrm_newpj.DBGDrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    var
      OldColor, OldFontColor: TColor;
    begin
      if CheckCurrencyField(Column.FieldName) then
      begin
        if (gdFocused in State) then
        begin
          sncDBCurrencyEdit.Left := Rect.Left + DBG.Left+2;
          sncDBCurrencyEdit.Top := Rect.Top + DBG.top+2;
          sncDBCurrencyEdit.Width := Rect.Right-Rect.Left;
          sncDBCurrencyEdit.Height := Rect.Bottom- Rect.Top;
          SetHighLightColor;
          if (sncDBCurrencyEdit.Tag=1) and not sncDBCurrencyEdit.Visible then
            sncDBCurrencyEdit.Visible := True;
        end
        else
          begin
            OldColor := sncDBCurrencyEdit.Color;
            OldFontColor := sncDBCurrencyEdit.Font.Color;
            SetNormalColor;
            DrawCurrencyFrame(DBG.Canvas, Rect, sncDBCurrencyEdit, Column.Field.AsFloat);
            sncDBCurrencyEdit.Color := OldColor;
            sncDBCurrencyEdit.Font.Color := OldFontColor;
          end;
      end;
    end;
      

  2.   

    procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
      Field: TField; State: TGridDrawState);
    begin
       if (gdFocused in State) then
       begin
       ComboBox1.Left:=Rect.Left+DBGrid1.Left;
       ComboBox1.Top:=Rect.Top+DBGrid1.Top;
       ComboBox1.Width:=Rect.Right-Rect.Left;
       ComboBox1.Height:=Rect.Bottom-Rect.Top;
       ComboBox1.Visible:=true;
       end;
    end;//////////////////////////////////////////////////
    这样就可以了