比如嵌入一个下拉列表框或者其他控件,如果您能够调试通过解决办法请答复,照搬DBGRID或者GRID的答复就不麻烦您贴了

解决方案 »

  1.   

    这是一个dbgrid的例子 你可以参考一下
    procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
      Field: TField; State: TGridDrawState);
    begin
    {  If Field.Index = 4 then
        DBGrid1.Canvas.FillRect(Rect); }
      if (gdFocused in State) then
      begin
         if (Field.FieldName = DBLookupCombo1.DataField) then
         begin
           DBLookupCombo1.Left := Rect.Left + DBGrid1.Left;
           DBLookupCombo1.Top := Rect.Top + DBGrid1.top;
           DBLookupCombo1.Width := Rect.Right - Rect.Left;
           DBLookupCombo1.Height := Rect.Bottom - Rect.Top;
           DBLookupCombo1.Visible := True;
         end
         else if (Field.FieldName = DBCheckBox1.DataField) then
         begin
           DBCheckBox1.Left := Rect.Left + DBGrid1.Left + 1;
           DBCheckBox1.Top := Rect.Top + DBGrid1.top + 1;
           DBCheckBox1.Width := Rect.Right - Rect.Left{ - 1};
           DBCheckBox1.Height := Rect.Bottom - Rect.Top{ - 1};
           DBCheckBox1.Visible := True;
         end
         else if (Field.FieldName = DBComboBox1.DataField) then
         begin
           DBComboBox1.Left := Rect.Left + DBGrid1.Left;
           DBComboBox1.Top := Rect.Top + DBGrid1.top;
           DBComboBox1.Width := Rect.Right - Rect.Left;
           DBComboBox1.Height := Rect.Bottom - Rect.Top;
           DBComboBox1.Visible := True;
         end
      end
      else {in this else area draw any stay behind bit maps}
      begin
        if (Field.FieldName = DBCheckBox1.DataField) then
        begin
          if TableGridDataCheckBox.AsBoolean then
            DBGrid1.Canvas.Draw(Rect.Left,Rect.Top, ImageTrue.Picture.Bitmap)
          else
            DBGrid1.Canvas.Draw(Rect.Left,Rect.Top, ImageFalse.Picture.Bitmap)
          {  DBGrid1.Canvas.StretchDraw(Rect, ImageFalse.Picture.Bitmap); }
        end
        { Do bit map dwawing if you want}
       { DBGrid1.Canvas.FillRect(Rect); }
      end;end;procedure TForm1.DBGrid1KeyPress(Sender: TObject; var Key: Char);
    begin
      if (key <> chr(9)) then
      begin
        if (DBGrid1.SelectedField.FieldName = DBLookupCombo1.DataField) then
        begin
          DBLookupCombo1.SetFocus;
          SendMessage(DBLookupCombo1.Handle, WM_Char, word(Key), 0);
        end
        else if (DBGrid1.SelectedField.FieldName = DBCheckBox1.DataField) then
        begin
          DBCheckBox1.SetFocus;
          SendMessage(DBCheckBox1.Handle, WM_Char, word(Key), 0);
        end
        else if (DBGrid1.SelectedField.FieldName = DBComboBox1.DataField) then
        begin
          DBComboBox1.SetFocus;
          SendMessage(DBComboBox1.Handle, WM_Char, word(Key), 0);
        end;
      end;
    end;
    procedure TForm1.DBGrid1ColExit(Sender: TObject);
    begin
      If DBGrid1.SelectedField.FieldName = DBLookupCombo1.DataField then
        DBLookupCombo1.Visible := false
      else If DBGrid1.SelectedField.FieldName = DBCheckBox1.DataField then
        DBCheckBox1.Visible := false
      else If DBGrid1.SelectedField.FieldName = DBComboBox1.DataField then
        DBComboBox1.Visible := false;
    end;
      

  2.   

    在Delphi的DBGrid中插入其他可视组件    Delphi提供了功能强大的 DBGrid组件,以方便进行数据库应用程序设计。但是如果我们仅仅利用DBGrid组件,每一个获得焦点(Grid)只是一个简单的文本编辑框,不方便用户输入数据。Delphi也提供了一些其他数据组件来方便用户输入,比如DBComboBox,DBCheckBox等组件,但这些组件却没有DBGrid功能强大。Delphi能不能象Visual Foxpro那样让DBGrid中获得焦点网格可以是其它可视数据组件以方便用户呢?其实我们可以通过在DBGrid中插入其他可视组件来实现这一点。   Delphi对DBGrid处理的内部机制,就是在网格上浮动一个组件——DBEdit组件。你输入数据的网格其实是浮动DBEdit组件,其他未获得焦点地方不过是图像罢了。所以,在DBGrid中插入其他可视组件就是在网格上浮动一个可视组件。因此任何组件,包括从简单的DbCheckBox到复杂的对话框,都可以在DBGrid中插入。下面就是一个如何在DBGrid中插入DBComboBox组件的步骤,采用同样的办法可以插入其他组件。 1、在Delphi 中新建一个项目。 2、分别拖动的Data Access组件板上DataSource、Table,Data Controls组件板上DBGrid,DBComboBox四个组件到Form1上。 3、设置各个组件的属性如下:rcf1对象 属性 设定植
    Form1 Caption '在DBGrid中插入SpinEdit组件示例'
    DataSource1 DataSet Table1
    Table1 DatabaseName DBDEMOS
    TableName 'teacher.DBF'
    Active True
    DBGrid1 DataSource DataSource1
    DBComboBox1 DataField SEX
    DataSource DataSource1
    Visible False
    Strings Items. '男'| '女'注意:我在这里用了Teacher.dbf,那是反映教职工的性别,只能是“男”或者是“女”。 4、DrawDataCell事件是绘制单元格,当获得焦点网格所对应的字段与组合框所对应的字段一致时,移动组合框到获得焦点的网格上,并且使组合框可视,从而达到在DBGrid指定列上显示DBComboBox的功能。设置DBGrid1的OnDrawDataCell事件如下:procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect; Field: TField; State: TGridDrawState);
    begin
     if (gdFocused in State) then
     begin
       if (Field.FieldName = DBComboBox1.DataField ) then
       begin
         DBComboBox1.Left := Rect.Left + DBGrid1.Left;
         DBComboBox1.Top := Rect.Top + DBGrid1.top;
         DBComboBox1.Width := Rect.Right - Rect.Left;
         DBComboBox1.Height := Rect.Bottom - Rect.Top;
         DBComboBox1.Visible := True;
       end;
     end;
    end; 5、DBGrid指定单元格未获得焦点时不显示DBComboBox,设置DBGrid1的OnColExit事件如下:
    procedure TForm1.DBGrid1ColExit(Sender: TObject);
    begin
     If DBGrid1.SelectedField.FieldName = DBComboBox1.DataField then
       begin
         DBComboBox1.Visible := false;
       end;
    end; 6、当DBGrid指定列获得焦点时DrawDataCell事件只是绘制单元格,并显示DBComboBox,但是DBComboBox并没有获得焦点,数据的输入还是在单元格上进行。在DBGrid1的KeyPress事件中调用SendMessage这个 Windows API函数将数据输入传输到DBComboBox上,从而达到在DBComboBox上进行数据输入。因此还要设置KeyPress事件如下:procedure TForm1.DBGrid1KeyPress(Sender: TObject; var Key: Char);
    begin
     if (key < > chr(9)) then
     begin
       if (DBGrid1.SelectedField.FieldName =DBComboBox1.DataField) then
       begin
         DBComboBox1.SetFocus;
         SendMessage(DBComboBox1.Handle,WM_Char,word(Key),0);
       end;
     end;
    end;   程序在中文Windows 98,Delphi 4.015 下调试通过。 锁定DBGrid左边的列 
       我在使用 Delphi3 进行数据库编程的时候,希望 DBGRID 构件在显示数据的时候能象FoxPro 的 BROWSE 命令一样,锁定左边指定的几列不进行滚动,请问应用什么方法来实现?   我们知道 Delphi 的 TStringGrid 有一个属性 FixedCols 来指定不滚动的列。虽然TDBGrid 不能直接使用这一属性,但通过强制类型转换也可以首先这一功能,因为这两个类都来自 TCustomGrid 类。下面我们以 Delphi 3.0的 Demos\Db\CtrlGrid 为例来说明具体的用法。在这个例子的 TFmCtrlGrid.FormShow 过程中加入如下一行:    TStringGrid(DbGrid1).FixedCols := 2;    运行该程序,在左右移动各列时,Symbol 列不会移动。除了这种方法,也可以采用下面的方法:首先在 Form 声明部分加上   type TMyGrid = Class(TDBGrid) end;    然后在 TFmCtrlGrid.FormShow 过程中加入:    TMyGrid(DbGrid1).FixedCols := 2;    两者从形式上略有不同,但实质都是一样的。我们这里设置 FixedCols 为 2,这是因为在 DBGrid 构件最左侧有个指示列,如果你将 DBGrid 的 Options 属性的 dgIndicator 设为False,则应设置 FixedCols 为1。