当前位置: 技术问答>java相关
帮我哦,如何在程序运行时动态添加下拉列表框中的数据(在dbgrid里用)?
来源: 互联网 发布时间:2015-01-02
本文导语: 我想用dbgrid直接进行输入或修改,表名为employee,字段为code(number类型),birthday(date类型),title(varchar2类型)。 我想在点击dbgrid里code字段时,可以出现下拉框或列表框来显示原先数据表中有的内容以供选择,...
我想用dbgrid直接进行输入或修改,表名为employee,字段为code(number类型),birthday(date类型),title(varchar2类型)。
我想在点击dbgrid里code字段时,可以出现下拉框或列表框来显示原先数据表中有的内容以供选择,点击birthday字段时可以弹出类似datetimepicker的控件来选择日期。如何做到呢,请详细一些(包括语句和一些属性的设置)。
我想在点击dbgrid里code字段时,可以出现下拉框或列表框来显示原先数据表中有的内容以供选择,点击birthday字段时可以弹出类似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画上去。
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画上去。
|
给一个日期对话框例子:
//Form 的Text源码
object DateDlg: TDateDlg
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;
interface
uses
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.
//Form 的Text源码
object DateDlg: TDateDlg
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;
interface
uses
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.
|
1、Form上放四个元件,Table1(or Query1),Table2(or Query2),DataSource1,DBGrid1。
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的按钮,弹出时间选择对话框。
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的按钮,弹出时间选择对话框。