比如嵌入一个下拉列表框或者其他控件,如果您能够调试通过解决办法请答复,照搬DBGRID或者GRID的答复就不麻烦您贴了
解决方案 »
- 我在tPanel 组件上 添加了TSpeedButton 。但是TSpeedButton的Down属性用不了,怎么办?
- 请教:各位大哥这样的函数要怎么写?谢谢!
- 数据库备份的问题
- 在delphi中如何创建oracle存储过程
- 如何用delphi做成应用程序先qq一样在状态栏显示图标?(在线)
- 我在MDI窗体左边放了一个Outlookbar! 200分
- ADORTL60.BPL错误,救命啊!
- 中午好热,郁闷。。。散分,先来先得。。。。。。
- 请教一个关于数据集的问题
- 今天我写了几个中国象棋的函数,请各位高手指点指点,谢谢!
- 急:关于Delphi中TPanel面板组件的属性设置?
- 一个关于网络连接的问题,请各位专业人士指教!
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;
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。