由于上传数据库不方便,所以,就直接使用XML文件做数据库吧。
这是实现后的效果截图:
三级菜单对应的XML文件(数据库):
第一级:省/直辖市,对应的XML文件:Location_Province.xml
第二级:地/市级,对应的XML文件:Location_City.xml
第三级:县/区级,对应的XML文件:Location_Area.xml
以下为三个XML文件,其中第一级为全部,由于文件太大,所以第二、三级为部分XML代码片段。
(1)省/直辖市,对应的XML文件:Location_Province.xml
<?xml version="1.0" standalone="yes"?>标签:XML,xml,cmbBoxProvince,Combobox,110100,DataSet,Location,Net,下拉菜单 From: https://blog.51cto.com/JohnsonJu/6090156
<NewDataSet>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Table">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:int" minOccurs="0" />
<xs:element name="province" type="xs:string" minOccurs="0" />
<xs:element name="provinceID" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<Table>
<id>1</id>
<province>北京市</province>
<provinceID>110000</provinceID>
</Table>
<Table>
<id>2</id>
<province>天津市</province>
<provinceID>120000</provinceID>
</Table>
<Table>
<id>3</id>
<province>河北省</province>
<provinceID>130000</provinceID>
</Table>
<Table>
<id>4</id>
<province>山西省</province>
<provinceID>140000</provinceID>
</Table>
<Table>
<id>5</id>
<province>内蒙古自治区</province>
<provinceID>150000</provinceID>
</Table>
<Table>
<id>6</id>
<province>辽宁省</province>
<provinceID>210000</provinceID>
</Table>
<Table>
<id>7</id>
<province>吉林省</province>
<provinceID>220000</provinceID>
</Table>
<Table>
<id>8</id>
<province>黑龙江省</province>
<provinceID>230000</provinceID>
</Table>
<Table>
<id>9</id>
<province>上海市</province>
<provinceID>310000</provinceID>
</Table>
<Table>
<id>10</id>
<province>江苏省</province>
<provinceID>320000</provinceID>
</Table>
<Table>
<id>11</id>
<province>浙江省</province>
<provinceID>330000</provinceID>
</Table>
<Table>
<id>12</id>
<province>安徽省</province>
<provinceID>340000</provinceID>
</Table>
<Table>
<id>13</id>
<province>福建省</province>
<provinceID>350000</provinceID>
</Table>
<Table>
<id>14</id>
<province>江西省</province>
<provinceID>360000</provinceID>
</Table>
<Table>
<id>15</id>
<province>山东省</province>
<provinceID>370000</provinceID>
</Table>
<Table>
<id>16</id>
<province>河南省</province>
<provinceID>410000</provinceID>
</Table>
<Table>
<id>17</id>
<province>湖北省</province>
<provinceID>420000</provinceID>
</Table>
<Table>
<id>18</id>
<province>湖南省</province>
<provinceID>430000</provinceID>
</Table>
<Table>
<id>19</id>
<province>广东省</province>
<provinceID>440000</provinceID>
</Table>
<Table>
<id>20</id>
<province>广西壮族自治区</province>
<provinceID>450000</provinceID>
</Table>
<Table>
<id>21</id>
<province>海南省</province>
<provinceID>460000</provinceID>
</Table>
<Table>
<id>22</id>
<province>重庆市</province>
<provinceID>500000</provinceID>
</Table>
<Table>
<id>23</id>
<province>四川省</province>
<provinceID>510000</provinceID>
</Table>
<Table>
<id>24</id>
<province>贵州省</province>
<provinceID>520000</provinceID>
</Table>
<Table>
<id>25</id>
<province>云南省</province>
<provinceID>530000</provinceID>
</Table>
<Table>
<id>26</id>
<province>西藏自治区</province>
<provinceID>540000</provinceID>
</Table>
<Table>
<id>27</id>
<province>陕西省</province>
<provinceID>610000</provinceID>
</Table>
<Table>
<id>28</id>
<province>甘肃省</province>
<provinceID>620000</provinceID>
</Table>
<Table>
<id>29</id>
<province>青海省</province>
<provinceID>630000</provinceID>
</Table>
<Table>
<id>30</id>
<province>宁夏回族自治区</province>
<provinceID>640000</provinceID>
</Table>
<Table>
<id>31</id>
<province>新疆维吾尔自治区</province>
<provinceID>650000</provinceID>
</Table>
<Table>
<id>32</id>
<province>台湾省</province>
<provinceID>710000</provinceID>
</Table>
<Table>
<id>33</id>
<province>香港特别行政区</province>
<provinceID>810000</provinceID>
</Table>
<Table>
<id>34</id>
<province>澳门特别行政区</province>
<provinceID>820000</provinceID>
</Table>
</NewDataSet>第二级:地/市级,对应的XML文件:Location_City.xml
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Table">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:int" minOccurs="0" />
<xs:element name="city" type="xs:string" minOccurs="0" />
<xs:element name="cityID" type="xs:string" minOccurs="0" />
<xs:element name="father" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<Table>
<id>1</id>
<city>市辖区</city>
<cityID>110100</cityID>
<father>110000</father>
</Table>
<Table>
<id>2</id>
<city>县</city>
<cityID>110200</cityID>
<father>110000</father>
</Table>
<Table>
<id>3</id>
<city>市辖区</city>
<cityID>120100</cityID>
<father>120000</father>
</Table>
<Table>
<id>4</id>
<city>县</city>
<cityID>120200</cityID>
<father>120000</father>
</Table>
<!-- //这里还有N多个 -->
</NewDataSet>第三级:县/区级,对应的XML文件:Location_Area.xml
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Table">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:int" minOccurs="0" />
<xs:element name="area" type="xs:string" minOccurs="0" />
<xs:element name="areaID" type="xs:string" minOccurs="0" />
<xs:element name="father" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<Table>
<id>1</id>
<area>东城区</area>
<areaID>110101</areaID>
<father>110100</father>
</Table>
<Table>
<id>2</id>
<area>西城区</area>
<areaID>110102</areaID>
<father>110100</father>
</Table>
<Table>
<id>3</id>
<area>崇文区</area>
<areaID>110103</areaID>
<father>110100</father>
</Table>
<Table>
<id>4</id>
<area>宣武区</area>
<areaID>110104</areaID>
<father>110100</father>
</Table>
<Table>
<id>5</id>
<area>朝阳区</area>
<areaID>110105</areaID>
<father>110100</father>
</Table>
<Table>
<id>6</id>
<area>丰台区</area>
<areaID>110106</areaID>
<father>110100</father>
</Table>
<Table>
<id>7</id>
<area>石景山区</area>
<areaID>110107</areaID>
<father>110100</father>
</Table>
<Table>
<id>8</id>
<area>海淀区</area>
<areaID>110108</areaID>
<father>110100</father>
</Table>
<Table>
<id>9</id>
<area>门头沟区</area>
<areaID>110109</areaID>
<father>110100</father>
</Table>
<Table>
<id>10</id>
<area>房山区</area>
<areaID>110111</areaID>
<father>110100</father>
</Table>
<Table>
<id>11</id>
<area>通州区</area>
<areaID>110112</areaID>
<father>110100</father>
</Table>
<Table>
<id>12</id>
<area>顺义区</area>
<areaID>110113</areaID>
<father>110100</father>
</Table>
<Table>
<id>13</id>
<area>昌平区</area>
<areaID>110114</areaID>
<father>110100</father>
</Table>
<Table>
<id>14</id>
<area>大兴区</area>
<areaID>110115</areaID>
<father>110100</father>
</Table>
<Table>
<id>15</id>
<area>怀柔区</area>
<areaID>110116</areaID>
<father>110100</father>
</Table>
<Table>
<id>16</id>
<area>平谷区</area>
<areaID>110117</areaID>
<father>110100</father>
</Table>
<Table>
<id>17</id>
<area>密云县</area>
<areaID>110228</areaID>
<father>110200</father>
</Table>
<Table>
<id>18</id>
<area>延庆县</area>
<areaID>110229</areaID>
<father>110200</father>
</Table>
<!-- //这里还有N多个 -->
</NewDataSet>这里先讲一下三者之间的关系:三个文件中分别有<provinceID>110000</provinceID>
<cityID>110100</cityID>
<areaID>110229</areaID>
之类的ID号,它们都是唯一的。
而City,Area一级中都存在一个:<father>110000</father>之类的元素,分别与其父级相应ID号对应。
这样,三级菜单之间就关联起来了。
关键代码:为方便描述,示意一下放代码的区域:
namespace AgencyWingoon
{
public partial class CompanyInfoForm : Form
{
// A区
}
}
将DataSet, DataTable相关变量放在上述的A区内:
DataSet dsProvince = null;
DataSet dsCity = null;
DataSet dsArea = null; DataTable dtProvince = null;
DataTable dtCity = null;
DataTable dtArea = null;
Form_Load事件中:(也放A区内)
private void CompanyInfoForm_Load(object sender, EventArgs e)
{
//初始化下拉菜单:
InitLocation();
//下面是其他代码
// .....
}以下代码全部也放在A区内:
private void InitLocation()
{
string xmlFileLocation_Province = @"./Location/Location_Province.xml";
string xmlFileLocation_City = @"./Location/Location_City.xml";
string xmlFileLocation_Area = @"./Location/Location_Area.xml";
//从XML文件中读取记录,加载为DataSet
dsProvince = new DataSet();
dsProvince.ReadXml(xmlFileLocation_Province);
dtProvince = dsProvince.Tables[0]; dsCity = new DataSet();
dsCity.ReadXml(xmlFileLocation_City);
dtCity = dsCity.Tables[0]; dsArea = new DataSet();
dsArea.ReadXml(xmlFileLocation_Area);
dtArea = dsArea.Tables[0]; BindProvinceData();
}
// cmbBoxProvince 省/直辖市下拉菜单选项变化时的事件:
private void cmbBoxProvince_SelectedIndexChanged(object sender, EventArgs e)
{
BindCityData();
this.cmbBoxCity.SelectedIndex = 0;
BindAreaData();
this.cmbBoxArea.SelectedIndex = 0;
}
// cmbBoxCity 地/市级下拉菜单选项变化时的事件:
private void cmbBoxCity_SelectedIndexChanged(object sender, EventArgs e)
{
BindAreaData();
this.cmbBoxArea.SelectedIndex = 0;
}// 绑定省/直辖市数据
private void BindProvinceData()
{
cmbBoxProvince.DataSource = dtProvince;
cmbBoxProvince.DisplayMember = "province";
cmbBoxProvince.ValueMember = "provinceID";
}// 绑定地/市级数据
private void BindCityData()
{
string provinceId = dtProvince.Rows[this.cmbBoxProvince.SelectedIndex]["provinceID"].ToString();
dtCity.DefaultView.RowFilter = "father = " + provinceId;
cmbBoxCity.DataSource = dtCity.DefaultView;
cmbBoxCity.DisplayMember = "city";
cmbBoxCity.ValueMember = "cityID";
}// 绑定县/区级数据
private void BindAreaData()
{
string provinceId = dtProvince.Rows[this.cmbBoxProvince.SelectedIndex]["provinceID"].ToString();
dtCity.DefaultView.RowFilter = "father = " + provinceId;
string cityId = dtCity.DefaultView.ToTable().Rows[this.cmbBoxCity.SelectedIndex]["cityID"].ToString();
dtArea.DefaultView.RowFilter = "father = " + cityId;
cmbBoxArea.DataSource = dtArea.DefaultView;
cmbBoxArea.DisplayMember = "area";
cmbBoxArea.ValueMember = "areaID";
}
OK!