用dbgrid录入数据
dbgrid中对应3列
id time name
其中id已经确定了
id time name
1
2
3
4
显示在dbgrid中了,现在要添加time name字段
想要在dbgird的column【1】中嵌入maskedit控件,来限制时间格式的输入
如何嵌入?
达到,焦点不道该cell的时候,不显示maskedit,焦点道该cell时候才显示maskedit
???
dbgrid中对应3列
id time name
其中id已经确定了
id time name
1
2
3
4
显示在dbgrid中了,现在要添加time name字段
想要在dbgird的column【1】中嵌入maskedit控件,来限制时间格式的输入
如何嵌入?
达到,焦点不道该cell的时候,不显示maskedit,焦点道该cell时候才显示maskedit
???
很容易的, 为什么要嵌入----麻烦而且容易出错
你试试看初始化combobox1的visible为falseprocedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if gdfocused in state then
begin
if column.FieldName='Zh' then//你要让combobox显示的那列的字段名
begin
form1.ComboBox1.Left:=rect.Left+form1.DBGrid1.Left;
form1.ComboBox1.Top:=rect.Top+form1.DBGrid1.Top;
form1.ComboBox1.Width:=rect.Right-rect.Left;
form1.ComboBox1.Height:=rect.Bottom-rect.Top;
form1.ComboBox1.Visible:=true;
end;
end;
end;
移上去的时候,combobox出现下拉框
可是maskedit就不行,我把maskedit设置成short time类型 那么它应该
直接受4个integer,而且中间应该有“:”格开的
但是,它就是不出来阿!!
if column.FieldName='time' then// begin
with maskedit1 do
begin
left:=rect.Left +dbgrid2.Left;
top:=rect.Top +dbgrid2.Top;
width:=rect.Right-Rect.left;
height:=rect.Bottom -rect.Top;
visible:=true;
end;
end;
开始:
1、 在Delphi 4.0中新建一个项目。
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;