+ -
当前位置:首页 → 问答吧 → (跪求)dbgrid中加入有下拉框的控件,如combobox等

(跪求)dbgrid中加入有下拉框的控件,如combobox等

时间:2009-04-14

来源:互联网

我知道的可以用picklist,但是picklist好像是不能自动点开的,需要手动点击。这样我做输入的时候,想从数据库中做个类似的匹配,不是很不方便?所以求高手能够指点,如果picklist有弹开的属性请告诉我,如果没有,怎么加入这个控件?我在网上看了很多的加入控件的做法,例如常用的:
  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  
  begin  
  DBComboBox1.Visible := false;  
  end;  
  end;  
   
   
  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; {这里会报错,弹出not visible,不可见,不能获得焦点}
  SendMessage(DBComboBox1.Handle, WM_Char, word(Key), 0);  
  end;  
  end;  
  end; 
 请高手能够给我指点,我理解的是在在我的keypress时根本没有执行绘制表格单元,在setfocus之前需要重画?恳请大哥大姐帮帮迷途的小弟!  

作者: jonathan_worj   发布时间: 2009-04-14

DBGrid-Columns[1]-PickList...可以实现的

作者: rouqing   发布时间: 2009-04-14

picklist中的items中可以添加的,但是它不能,即时的现实下拉框的吧?

作者: jonathan_worj   发布时间: 2009-04-14

麻烦告诉我好吗?pickerlist怎么实现我输入时,下拉框能够自动打开?

作者: jonathan_worj   发布时间: 2009-04-14

procedure TForm1.DBGrid2DrawDataCell(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 + DBGrid2.Left;
  DBComboBox1.Top := Rect.Top + DBGrid2.top;
  DBComboBox1.Width := Rect.Right - Rect.Left;
  DBComboBox1.Height := Rect.Bottom - Rect.Top;
  DBComboBox1.Visible := True;
  SendMessage(DBComboBox1.Handle, CB_SHOWDROPDOWN , Integer(True), 0);//在这里加上这句
  end;
  end;
end;
把procedure TForm1.DBGrid1KeyPress(Sender: TObject; var Key: Char);事件中的代码去掉。  
 

作者: lovelymelon   发布时间: 2009-04-14

OK,我先试试

作者: jonathan_worj   发布时间: 2009-04-14

我想问下,pickerlist可以自己打开下拉框的吗?我是找了好久的,也没有发现可以打开下拉框。因为是刚学不久,所以请大家不吝赐教。还有例子上的做法应该是没有错的,为什么程序会报错了?难道不是在单元格获取焦点的时候,dbgrid就会绘制单元格吗?上面的大哥是不是在它DBComboBox1显示的时候将它弹开一个空的下拉框?

作者: jonathan_worj   发布时间: 2009-04-14

如果要想DBComboBox1下拉框有内容,可以自己写代码
Delphi(Pascal) code

  self.ADOQuery5.Close;
  self.ADOQuery5.Connection:=self.ADOConnection2;
  Self.ADOQuery5.SQL.Clear;
  Self.ADOQuery5.SQL.Add('select distinct bianhao from Tzhk_czp');
  self.ADOQuery5.Open;
  while not Self.ADOQuery5.Eof do
  begin
    sql:=Self.ADOQuery5.FieldByname('bianhao').AsString;
    self.DBComboBox1.Items.Add(sql);
    self.ADOQuery5.Next;
  end;

作者: lovelymelon   发布时间: 2009-04-14

再次感谢lovelymelon的推荐,剩下的我已经知道了,上面的问题能够给我个答复吗?谢谢了

作者: jonathan_worj   发布时间: 2009-04-14

dbgrideh好像可以,dbgrid以前没用个这个picklist,现在还没研究出来。

作者: lovelymelon   发布时间: 2009-04-14

我用的是dbgrideh,但是现在有个问题,我在现在碰到的程序中运行代码时,发现DrawDataCell根本没有运行啊,这是怎么回事?本来我在例子中是个table表的dataset,直接选的是表,运行可以的。麻烦了大哥

作者: jonathan_worj   发布时间: 2009-04-14

不会吧,我用的是adoquery读取数据啊,可以触发这个事件啊,你单步运行看看能不能触发这个事件,如果不能触发这个事件,看看你写代码的dbgrideh是你显示数据的那个吗?

作者: lovelymelon   发布时间: 2009-04-14

我用的就是Adoquery啊,但我由于是输入界面,在column中已经绑定了字段,我单步运行时,根本就没有触发DrawDataCell事件,真是奇怪啊,网上说不以绑定字段,我是输入界面,和绑定字段有关吗?

作者: jonathan_worj   发布时间: 2009-04-14

combobox 有个AutoDropDown属性 设置为true 实现自动下拉框

onchange事件中
procedure TForm1.ComboBox1Change(Sender: TObject);
 var p:string;
begin
if(combobox1.text<>'') then begin//为空的时候不触发 不然会再下拉框中填充所有数据
  with ADOQuery1 do
  begin
  close;
  SQL.Clear;
  SQL.Add('Select * FROM tb_company where CompanyName like :p');
  Parameters.ParamByName('p').Value:='%'+trim(combobox1.text)+'%';//查找匹配的数据
  open;
  end;
  combobox1.items.text:='';
  ComboBox1.SelStart:= Length(ComboBox1.Text);//确保光标会在后面 否则会自动跳到输入数据前面
 while not adoquery1.Eof do
  begin

  ComboBox1.Items.Add(adoquery1.Fieldbyname('CompanyName').AsString);//下拉框中把匹配的数据绑定
  adoquery1.Next;
  end;
  end;
end;


你参考下

作者: a381391536   发布时间: 2011-08-10

热门下载

更多