码迷,mamicode.com
首页 > 数据库 > 详细

DBLookupCombobox实现下拉联动

时间:2018-08-20 00:33:41      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:arch   src   打开   数据   enable   inf   代码   div   tab   

这次用DBLookupCombobox实现省份-城市-地区的下拉联动,用以学习DBLookupCombobox的一些用法

效果图:

技术分享图片

首先建立数据表(数据表形式可以多种多样:数据库表、文本、xml等,最终能转换成Dataset数据集就好,这里使用数据库表)

 

1.省份表

技术分享图片

2.城市表

技术分享图片

3.地区表

技术分享图片

 

时间关系,就简单做了几个数据,只有广东-广州-地区是完整的数据

建表及数据代码:

技术分享图片
USE [his_mz]
GO
/****** Object:  Table [dbo].[City]    Script Date: 2018-8-19 23:04:00 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[City](
    [code] [varchar](10) NOT NULL,
    [name] [varchar](50) NOT NULL,
    [province] [varchar](10) NOT NULL,
    [other] [varchar](50) NULL,
 CONSTRAINT [PK_City] PRIMARY KEY CLUSTERED 
(
    [code] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[district]    Script Date: 2018-8-19 23:04:00 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[district](
    [code] [varchar](10) NOT NULL,
    [name] [varchar](50) NOT NULL,
    [city] [varchar](10) NOT NULL,
    [other] [varchar](50) NULL
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[province]    Script Date: 2018-8-19 23:04:00 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[province](
    [code] [varchar](10) NOT NULL,
    [name] [varchar](50) NOT NULL,
    [other] [varchar](50) NULL
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[City] ([code], [name], [province], [other]) VALUES (Nc1001, N北京市, N1001, NULL)
GO
INSERT [dbo].[City] ([code], [name], [province], [other]) VALUES (Nc1002, N上海市, N1001, NULL)
GO
INSERT [dbo].[City] ([code], [name], [province], [other]) VALUES (Nc1003, N天津市, N1001, NULL)
GO
INSERT [dbo].[City] ([code], [name], [province], [other]) VALUES (Nc1004, N重庆市, N1001, NULL)
GO
INSERT [dbo].[City] ([code], [name], [province], [other]) VALUES (Nc1005, N广州市, N1002, NULL)
GO
INSERT [dbo].[City] ([code], [name], [province], [other]) VALUES (Nc1006, N深圳市, N1002, NULL)
GO
INSERT [dbo].[City] ([code], [name], [province], [other]) VALUES (Nc1007, N珠海市, N1002, NULL)
GO
INSERT [dbo].[City] ([code], [name], [province], [other]) VALUES (Nc1008, N南宁市, N1003, NULL)
GO
INSERT [dbo].[City] ([code], [name], [province], [other]) VALUES (Nc1009, N玉林市, N1003, NULL)
GO
INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (Nd1001, N越秀区, Nc1005, NULL)
GO
INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (Nd1002, N荔湾区, Nc1005, NULL)
GO
INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (Nd1003, N天河区, Nc1005, NULL)
GO
INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (Nd1004, N海珠区, Nc1005, NULL)
GO
INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (Nd1005, N黄浦区, Nc1005, NULL)
GO
INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (Nd1006, N萝岗区, Nc1005, NULL)
GO
INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (Nd1007, N芳村区, Nc1005, NULL)
GO
INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (Nd1008, N白云区, Nc1005, NULL)
GO
INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (Nd1009, N花都区, Nc1005, NULL)
GO
INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (Nd1010, N增城区, Nc1005, NULL)
GO
INSERT [dbo].[district] ([code], [name], [city], [other]) VALUES (Nd1011, N从化区, Nc1005, NULL)
GO
INSERT [dbo].[province] ([code], [name], [other]) VALUES (N1001, N直辖市, NULL)
GO
INSERT [dbo].[province] ([code], [name], [other]) VALUES (N1002, N广东省, NULL)
GO
INSERT [dbo].[province] ([code], [name], [other]) VALUES (N1003, N广西壮族自治区, NULL)
GO
建表及数据SQL

 

窗体大概放这些控件,都是Delphi自带的。(如果安装了Ehlib包,用DBLookupComboboxEh效果更佳)

 技术分享图片

DBLookupCombobox的属性设置及说明

技术分享图片

ListSource:下拉列表的数据集

ListField:下拉列表要显示的字段的名称

KeyField:要获得的字段值。例如广东对应的记录,1002,广东省。如果广东省被选择了,那么DBLookupCombobox.KeyValue的值就取字段name为广东的那条记录的code值(1002)

初始化数据集

技术分享图片
procedure TForm2.FormCreate(Sender: TObject);
begin
  //初始化数据集信息
  qryProvince.Close;
  qryCity.Close;
  qryDistrict.Close;

  qryProvince.SQL.Text := select * from province;
  qryCity.SQL.Text := select * from city;
  qryDistrict.SQL.Text := select * from district;

  qryProvince.Open;
  qryCity.Open;
  qryDistrict.Open;

  //打开数据集
  qryProvince.Open;
  qryCity.Open;
  qryDistrict.Open;

  //初始化DBLookupcombobox
  qryCity.Filtered := False;
  qryCity.Filter := code=+QuotedStr(‘‘);
  qryCity.Filtered := True;

  qryDistrict.Filtered := False;
  qryDistrict.Filter := code=+QuotedStr(‘‘);
  qryDistrict.Filtered := True;
end;
初始化数据集

当省份选择之后,触发DBLookupCombobox的CloseUp事件

技术分享图片
procedure TForm2.cbProvinceCloseUp(Sender: TObject);
var
  keycode: string;
begin
  if cbProvince.Text=EmptyStr then
    keycode := EmptyStr
  else
    keycode := cbProvince.KeyValue; //关键是keyvalue,keyvalue的值来自于keyField,可以在属性面板设置keyFiled

  //使用filter过滤,只显示Province=keycode的数据集
  qryCity.Filtered := False;
  qryCity.Filter := Province=+QuotedStr(keycode);
  qryCity.Filtered := True;

  //清除城市下拉框已选的值
  cbCity.KeyValue := ‘‘;
  cbDistrict.KeyValue := ‘‘;
end;
当省份选择之后

当城市选择之后

技术分享图片
procedure TForm2.cbCityCloseUp(Sender: TObject);
var
  keycode: string;
begin
  if cbCity.Text=EmptyStr then                
    keycode := EmptyStr
  else
    keycode := cbCity.KeyValue;
  //使用filter过滤,只显示city=keycode的数据集
  qryDistrict.Filtered := False;
  qryDistrict.Filter := city=+QuotedStr(keycode);
  qryDistrict.Filtered := True;
  //由于有些城市的区域为空,所以要把DBLookupCombobox的enable设置FALSE,否则会报错
  if qryDistrict.RecordCount=0 then
    cbDistrict.Enabled := False
  else
    cbDistrict.Enabled := True;

  //清除已选择的地区的值
  cbDistrict.KeyValue := ‘‘;
end;
城市选择之后

 

DBLookupCombobox实现下拉联动

标签:arch   src   打开   数据   enable   inf   代码   div   tab   

原文地址:https://www.cnblogs.com/Kwong/p/9503315.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!