还有一个问题:怎么实现在dbgrid里新增记录啊,我用select * from employee,打开后dbgrid就是显示原先数据表中的字段内容,可是我发现不能新增的,而且我想要的是只有employee的字段名,然后是一行空白的行,以供我进行新增操作,一行填好,按回车又可以出现第二行空白行,如何实现呀?
动态添加下拉列表框中的数据: dbgrid1.columns[i].picklist.clear; with table1 do begin first; while not eof do begin dbgrid1.columns[i].picklist.add(table1.fieldbyname('field').asstring); next; end; end; 你使用的是query吧: query.requestlive:=true; 如果没有设置只读属性应该是可以新增的。 至于datetimepicker; 可以添加一个datetimepicker控件,再通过dbgrid.drawccolumncell事件把datetimepicker画上去。
dbgrid1.columns[i].picklist.clear;
with table1 do
begin
first;
while not eof do
begin
dbgrid1.columns[i].picklist.add(table1.fieldbyname('field').asstring);
next;
end;
end;
你使用的是query吧:
query.requestlive:=true;
如果没有设置只读属性应该是可以新增的。
至于datetimepicker;
可以添加一个datetimepicker控件,再通过dbgrid.drawccolumncell事件把datetimepicker画上去。
我是用一个按钮控制table的applyupdates,在dbgrid中倒是可以增加行数并允许写入内容的,可是激发了按钮事件,只有修改的内容保存了,新增的行却没有存到数据表中。
还有一个问题,怎么把datatimepicker画上去呀,你能不能写点代码给我看看呀,谢谢
Table1和Table2的TableName=employee;
DataSource1.DataSet=Table2;
DBGrid1.DataSource=DataSource1.
2、打开Table1字段编辑器,增加要显示的字段后,新增一Lookup字段:
Name=CODE1;Type=Integer;Field Type=Lookup;DataSet=Table2;Key Fields=CODE;Lookup Fields=CODE;Result Field=CODE。
3、编辑DBGrid1.Columns属性,增加要显示的字段,包括CODE和CODE1,birthday字段的ButtonStyle=cbsEllipsis;
4、响应DBGrid1.OnEditButtonClick事件,在其中调用你编写的日期选择对话框,将日期值赋给birthday字段。
运行之,新增代码在CODE字段,选择原表已有代码点击CODE1的下拉框,点击birthday的按钮,弹出时间选择对话框。
Left = 271
Top = 159
BorderStyle = bsNone
Caption = '时间选择'
ClientHeight = 184
ClientWidth = 271
Color = clBtnFace
Font.Charset = GB2312_CHARSET
Font.Color = clWindowText
Font.Height = -12
Font.Name = '宋体'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 12
object Panel1: TPanel
Left = 0
Top = 0
Width = 271
Height = 184
Align = alClient
BevelInner = bvRaised
Caption = 'Panel1'
TabOrder = 0
object MonthCalendar1: TMonthCalendar
Left = 2
Top = 2
Width = 267
Height = 143
Align = alTop
Date = 36988.7495943287
TabOrder = 0
end
object BitBtn1: TBitBtn
Left = 120
Top = 152
Width = 65
Height = 25
Caption = '确认'
TabOrder = 1
Kind = bkOK
end
object BitBtn2: TBitBtn
Left = 192
Top = 152
Width = 65
Height = 25
Caption = '取消'
TabOrder = 2
Kind = bkCancel
end
end
end
//单元源码
unit DateSet;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons, Spin, Grids, Calendar, ComCtrls, ExtCtrls;type
TDateDlg = class(TForm)
Panel1: TPanel;
MonthCalendar1: TMonthCalendar;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
function GetDate: TDateTime;
procedure SetDate(aDate: TDateTime);
private
{ Private declarations }
public
{ Public declarations }
property Date: TDateTime read GetDate write SetDate;
end;var
DateDlg: TDateDlg;implementation{$R *.DFM}function TDateDlg.GetDate: TDateTime;
begin
Result := MonthCalendar1.Date;
end;procedure TDateDlg.SetDate(aDate: TDateTime);
begin
MonthCalendar1.Date := aDate;
end;end.