数据库有三张表: 国家 省份 城市 三张表有相应的关系
每个表都是 ID Name
要求用三个comboBox对应着三个表来做选择
每张表里还要在程序中添加一个 “全部”项怎样让这三个comboBox关联起来 即 A影响BC B影响C还要考虑选择“全部”的问题
每个表都是 ID Name
要求用三个comboBox对应着三个表来做选择
每张表里还要在程序中添加一个 “全部”项怎样让这三个comboBox关联起来 即 A影响BC B影响C还要考虑选择“全部”的问题
DataRowView rowView = (DataRowView)comboBox1.SelectedItem;
string couID = rowView.Row["countryID"].ToString();
取出的国家ID去数据库中取出相应的省绑定到comboBox2根据省取城市同样道理
-----------------------
select的时候,不指定条件或是加类似于where 1=1这样的条件时就是全部了ComboBox绑定后不能添加item,可以在select的时候添加,也可以在填充到DataSet后,添加一个DataRow再绑定另外,个人觉得加“全部”并不好,国家选全部,省再选全部,那城市就要全读出来,有那个必要吗
{
try
{
int nation = (int)Helper.GetComboBoxSelectedValue(this.cmbNation);
this.CantonID = nation;
this.InternalBindProvince(nation);
}
catch
{
//
}
} private void cmbProvince_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
int province = (int)Helper.GetComboBoxSelectedValue(this.cmbProvince);
this.CantonID = province;
this.InternalBindCity(province);
}
catch
{
//
}
} private void cmbCity_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
int city = (int)Helper.GetComboBoxSelectedValue(this.cmbCity);
this.CantonID = city;
this.InternalBindCounty(city);
}
catch
{
//
}
} private void cmbCounty_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
int county = (int)Helper.GetComboBoxSelectedValue(this.cmbCounty);
this.CantonID = county;
}
catch
{
//
}
} #endregion /// <summary>
/// 绑定籍贯数据(国籍)到下拉列表框。
/// </summary>
protected void BindNation()
{
SunnyHIS.Integral.DataCenter.DAL.Interface.ICantonList list = SunnyHIS.Integral.DataCenter.DAL.Interface.DALHelper.DALManager.CreateCantonList();
list.Session = this.Session;
list.GetCantonNationList(); this.InternalBindNativePlace(list, this.cmbNation);
} /// <summary>
/// 绑定指定国家的省份数据到下拉列表。
/// </summary>
internal void InternalBindProvince(int nation)
{
SunnyHIS.Integral.DataCenter.DAL.Interface.ICantonList list = SunnyHIS.Integral.DataCenter.DAL.Interface.DALHelper.DALManager.CreateCantonList();
list.Session = this.Session;
list.GetCantonProvinceList(nation); this.InternalBindNativePlace(list, this.cmbProvince);
} /// <summary>
/// 绑定指定省份的城市数据到下拉列表。
/// </summary>
internal void InternalBindCity(int province)
{
SunnyHIS.Integral.DataCenter.DAL.Interface.ICantonList list = SunnyHIS.Integral.DataCenter.DAL.Interface.DALHelper.DALManager.CreateCantonList();
list.Session = this.Session;
list.GetCantonCityList(province); this.InternalBindNativePlace(list, this.cmbCity);
} /// <summary>
/// 绑定指定省份的城市数据到下拉列表。
/// </summary>
internal void InternalBindCounty(int city)
{
SunnyHIS.Integral.DataCenter.DAL.Interface.ICantonList list = SunnyHIS.Integral.DataCenter.DAL.Interface.DALHelper.DALManager.CreateCantonList();
list.Session = this.Session;
list.GetCantonCountyList(city); this.InternalBindNativePlace(list, this.cmbCounty);
} internal void InternalBindNativePlace(SunnyHIS.Integral.DataCenter.DAL.Interface.ICantonList list, System.Windows.Forms.ComboBox cbx)
{
cbx.Items.Clear(); System.Collections.ArrayList al = new System.Collections.ArrayList(); foreach(SunnyHIS.Integral.DataCenter.DAL.Interface.ICanton var in list.Rows)
{
cbx.Items.Add(var.Name);
al.Add(var.Code);
} cbx.Tag = al; if(cbx.Items.Count > 0)
{
cbx.SelectedIndex = 0;
}
}
用多个ComboBox级联,又要全部填充,这本身是个“矛盾”的事情,因为ComboBox总会“选择”一项!不如你换一个思路:用TreeView+DataGridView/ListView!
先在数据库中建立一个三表的联合查询,然后根据国家、省份、城市的不同级别动态建立树节点。最后根据选择的不同树节点,把相关的信息动态填充动右边的DataGridView/ListView中!
用多个ComboBox级联,又要全部填充,这本身是个“矛盾”的事情,因为ComboBox总会“选择”一项!不如你换一个思路:用TreeView+DataGridView/ListView!
先在数据库中建立一个三表的联合查询,然后根据国家、省份、城市的不同级别动态建立树节点。最后根据选择的不同树节点,把相关的信息动态填充动右边的DataGridView/ListView中!
就用SelectedIndexChanged写程序处理