求助:點擊DBGrid中CELL下面的下拉框觸發什麼事件?當在DBGrid中的ButtonStyle為:cbsAuto時,PickList有值時,用何事件來觸發下拉框?
例如:當進入一格時,下拉框自動彈出?

解决方案 »

  1.   

    楼主你好:
        我从昨天晚上到今天凌晨,对这个问题整整研究了一夜,以为网格中的TDBGridInplaceEdit类是Tcombobox的子类,试图向它发送各种消息让其展开,但都不成功,差点晕死。后来在网上找到资料,原来TDBGridInplaceEdit不是公开的类,它的父类是TInplaceEdit。
        下面是用空格键展开DBGrid网格中的PickList的方法:
    --------------------------------------------------------------------
    {
    作者: creation_zy
    日期: 2006-3-9
    }
    procedure TForm1.DBGrid1KeyPress(Sender: TObject; var Key: Char);
    var
      i:Integer;
      MyEdit:TInplaceEdit;
      x,y:Integer;
      P:TPoint;
    begin
      if Key=' ' then//在此还需要加入判断当前列是否能够下拉的逻辑,以免空格键都失效(根据当前列的PickList是否为空)
      begin
        for i:=0 to Pred(DBGrid1.ComponentCount) do
          if DBGrid1.Components[i] is TInplaceEdit then
          begin
            Key:=#0;
            MyEdit:=TInplaceEdit(DBGrid1.Components[i]);
            x:=MyEdit.BoundsRect.Right-MyEdit.BoundsRect.Left-4;
            y:=4;
            PostMessage(MyEdit.Handle,WM_LBUTTONDOWN,1,x+y shl 16);
            PostMessage(MyEdit.Handle,WM_LBUTTONUP,0,x+y shl 16);
            break;
          end;
      end;
    end; 
    --------------------------------------------------------------------
    实际上,我觉得我们在这个问题上花费太多精力不值得(你上一个类似贴子我也回过)
    如果用空格键展开PickList你认为不方便(表格录入时,纯键盘操作其实也挺方便的),可以抛开这种思路,单纯地利用嵌入DBcombobox控件就可以很简单地搞定。下面是代码:
    ---------------------------------------------------------------------
    //DBCombobox1.datafield == 列的datafield
    //DBCombobox1.visible:=false
    //不能在设计阶段画columns,只能由DBGrid自动画
    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; procedure TForm1.DBGrid1ColExit(Sender: TObject); 
      begin 
         If DBGrid1.SelectedField.FieldName = DBComboBox1.DataField then 
              DBComboBox1.Visible := false; 
      end; procedure TForm1.DBGrid1KeyPress(Sender: TObject; var Key: Char); 
      begin 
         if (key <> chr(9)) then 
              if (DBGrid1.SelectedField.FieldName =DBComboBox1.DataField) then 
                 begin 
                 DBComboBox1.SetFocus; 
                 SendMessage(DBComboBox1.Handle, WM_Char, word(Key), 0); 
                 end; 
      end; 
    -------------------------------------------------------------------------------呵呵,我还差专家分82分就变成三条裤衩了,施舍一下!