效果图:
//三个Combobox控件:国家-cmbCountry 省份-cmbProvince 地区:cmbArea
首先,初始化窗体控件值(加载国家列表)
编辑cmbCountry的SelectedIndexChanged控件
同时编辑cmbProvince的SelectedIndexChanged控件
至此,就能实现我们所要的目的了。
编辑时,只需直接给它赋值即可
总结:
这里值得注意的应该就是选择国家或省份时值切换时该使用的辅助对象DataRowView。这里不具体说明,如果有疑问的朋友,欢迎回复讨论!
//三个Combobox控件:国家-cmbCountry 省份-cmbProvince 地区:cmbArea
首先,初始化窗体控件值(加载国家列表)
Code
/// <summary>
/// 初始化窗体控件值
/// </summary>
private void InitializeEnviroment()
{
try
{
byte[] byteDataset = Services.BASService.GetCountryList();
DataSet ds = Converter.DecompressDS(byteDataset);
cmbCountry.DataSource = ds.Tables[0];
cmbCountry.DisplayMember = "Country";
for (int i = 0; i < cmbCountry.Items.Count; i++)
{
DataRowView item = (DataRowView)cmbCountry.Items[i];
if (item.Row[0].ToString() == "中国")
{
cmbCountry.SelectedIndex = i;
break;
}
}
//if (cmbCountry.Items.Count > 0)
// cmbCountry.SelectedIndex = 0;
}
catch(Exception ex)
{
MessageBox.Show(Definition.MESSAGE_QUERY_ERROR + ":" + ex.Message);
}
}
/// <summary>
/// 初始化窗体控件值
/// </summary>
private void InitializeEnviroment()
{
try
{
byte[] byteDataset = Services.BASService.GetCountryList();
DataSet ds = Converter.DecompressDS(byteDataset);
cmbCountry.DataSource = ds.Tables[0];
cmbCountry.DisplayMember = "Country";
for (int i = 0; i < cmbCountry.Items.Count; i++)
{
DataRowView item = (DataRowView)cmbCountry.Items[i];
if (item.Row[0].ToString() == "中国")
{
cmbCountry.SelectedIndex = i;
break;
}
}
//if (cmbCountry.Items.Count > 0)
// cmbCountry.SelectedIndex = 0;
}
catch(Exception ex)
{
MessageBox.Show(Definition.MESSAGE_QUERY_ERROR + ":" + ex.Message);
}
}
编辑cmbCountry的SelectedIndexChanged控件
Code
private void cmbCountry_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
DataRowView rowC = (DataRowView)cmbCountry.SelectedItem;
byte[] byteDataset = Services.BASService.GetProvinceList(rowC.Row[0].ToString());
DataSet ds = Converter.DecompressDS(byteDataset);
cmbProvince.DataSource = ds.Tables[0];
cmbProvince.DisplayMember = "Province";
for (int i = 0; i < cmbProvince.Items.Count; i++)
{
DataRowView item = (DataRowView)cmbProvince.Items[i];
if (item.Row[0].ToString() == "福建")
{
cmbProvince.SelectedIndex = i;
break;
}
}
}
catch (Exception ex)
{
MessageBox.Show(Definition.MESSAGE_QUERY_ERROR + ":" + ex.Message);
}
}
private void cmbCountry_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
DataRowView rowC = (DataRowView)cmbCountry.SelectedItem;
byte[] byteDataset = Services.BASService.GetProvinceList(rowC.Row[0].ToString());
DataSet ds = Converter.DecompressDS(byteDataset);
cmbProvince.DataSource = ds.Tables[0];
cmbProvince.DisplayMember = "Province";
for (int i = 0; i < cmbProvince.Items.Count; i++)
{
DataRowView item = (DataRowView)cmbProvince.Items[i];
if (item.Row[0].ToString() == "福建")
{
cmbProvince.SelectedIndex = i;
break;
}
}
}
catch (Exception ex)
{
MessageBox.Show(Definition.MESSAGE_QUERY_ERROR + ":" + ex.Message);
}
}
同时编辑cmbProvince的SelectedIndexChanged控件
Code
private void cmbProvince_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
DataRowView rowC = (DataRowView)cmbCountry.SelectedItem;
DataRowView rowP = (DataRowView)cmbProvince.SelectedItem;
byte[] byteDataset = Services.BASService.GetAreaList(rowC.Row[0].ToString(), rowP.Row[0].ToString());
DataSet ds = Converter.DecompressDS(byteDataset);
cmbArea.DataSource = ds.Tables[0];
cmbArea.DisplayMember = "Area";
for (int i = 0; i < cmbArea.Items.Count; i++)
{
DataRowView item = (DataRowView)cmbArea.Items[i];
if (item.Row[0].ToString() == "厦门")
{
cmbArea.SelectedIndex = i;
break;
}
}
}
catch (Exception ex)
{
MessageBox.Show(Definition.MESSAGE_QUERY_ERROR + ":" + ex.Message);
}
}
private void cmbProvince_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
DataRowView rowC = (DataRowView)cmbCountry.SelectedItem;
DataRowView rowP = (DataRowView)cmbProvince.SelectedItem;
byte[] byteDataset = Services.BASService.GetAreaList(rowC.Row[0].ToString(), rowP.Row[0].ToString());
DataSet ds = Converter.DecompressDS(byteDataset);
cmbArea.DataSource = ds.Tables[0];
cmbArea.DisplayMember = "Area";
for (int i = 0; i < cmbArea.Items.Count; i++)
{
DataRowView item = (DataRowView)cmbArea.Items[i];
if (item.Row[0].ToString() == "厦门")
{
cmbArea.SelectedIndex = i;
break;
}
}
}
catch (Exception ex)
{
MessageBox.Show(Definition.MESSAGE_QUERY_ERROR + ":" + ex.Message);
}
}
至此,就能实现我们所要的目的了。
编辑时,只需直接给它赋值即可
cmbCountry.Text = dr["Country"].toString();
cmbProvince.Text = dr["Province"].toString();
cmbArea.Text = dr["Area"].toString();
cmbProvince.Text = dr["Province"].toString();
cmbArea.Text = dr["Area"].toString();
总结:
这里值得注意的应该就是选择国家或省份时值切换时该使用的辅助对象DataRowView。这里不具体说明,如果有疑问的朋友,欢迎回复讨论!