两个ComboBox的内容关联
来源:互联网 发布:重庆旅游网络推广 编辑:程序博客网 时间:2024/05/16 05:35
在用户注册或者是查询时通常都会用到两个下拉框的关联操作。当在一个下拉框中选择安徽,另个下拉框立即出现安徽的城市。选择江苏,则江苏的城市会出现。其实这个也就是数据库的绑定问题。好了,废话不说了。首先要设计数据库,这个问题就不用我说了吧。要弄好省和城市的关联。从数据库中把数据提取出来我也不说了,现在把省和城市的数据分别放到dataset类型的ds1和ds2中。
首先:在构造函数中写一个方法把省份填充进去。
string[] usergroup = new string[ds1.Tables[0].Rows.Count];
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
usergroup[i] = ds.Tables[0].Rows[i][0].ToString();
}
this.comboBox1.Items.AddRange(usergroup);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
usergroup[i] = ds.Tables[0].Rows[i][0].ToString();
}
this.comboBox1.Items.AddRange(usergroup);
当选择其中一个省份的时候就会触发一个comboBox1_SelectedIndexChanged事件,然后从数据库中把相应的城市提取出来,在绑定到ComboBox2中就可以了。
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
this.comboBox2.Items.Clear();
int index = this.comboBox1.SelectedIndex;
string strconn = "server=(local);" + "integrated security=SSPI;" + "database=Student;";
SqlConnection conn = new SqlConnection(strconn);
SqlDataAdapter da = new SqlDataAdapter("select city from city where ShengID=1+"+index, conn);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
//comboBox1.DataSource = ds.Tables[0];
string[] usergroup = new string[ds.Tables[0].Rows.Count];
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
usergroup[i] = ds.Tables[0].Rows[i][0].ToString();
}
this.comboBox2.Text = usergroup[0];
this.comboBox2.Items.AddRange(usergroup);
}
{
this.comboBox2.Items.Clear();
int index = this.comboBox1.SelectedIndex;
string strconn = "server=(local);" + "integrated security=SSPI;" + "database=Student;";
SqlConnection conn = new SqlConnection(strconn);
SqlDataAdapter da = new SqlDataAdapter("select city from city where ShengID=1+"+index, conn);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
//comboBox1.DataSource = ds.Tables[0];
string[] usergroup = new string[ds.Tables[0].Rows.Count];
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
usergroup[i] = ds.Tables[0].Rows[i][0].ToString();
}
this.comboBox2.Text = usergroup[0];
this.comboBox2.Items.AddRange(usergroup);
}
行了,原理很简单,操作起来其实也很简单。
- 两个ComboBox的内容关联
- 两个easyui combobox 逻辑关联
- combobox下拉列表控件 关联绑定两个表的技巧
- 请教:关于Stringgird和combobox的关联
- easyui生成动态关联的combobox
- 两个dropdownlist 的关联
- 两个表的关联
- Ext.form.ComboBox 实现两个ComboBox 的相互级联
- 如何实现两个或多个Spinner之间内容的关联
- 简单的两个Spinner内容相关联,Spinner与TextView关联
- extjs4.1:两个combobox的联动
- 【WPF】两个下拉列表ComboBox的级联
- 取出combobox内容的索引值
- 关联表的两个代码
- 两个DataTable的关联问题
- 两个表的关联查询
- 父类的combobox与子类的combobox的关联实现方式
- ComboBox绑定两个字段
- PostBack后让滚动条回到源位
- Web.config文件的加密方法
- 让人感动的英文句子
- XmlDocument 操作xml文档
- 格式化日期、货币和其它数据
- 两个ComboBox的内容关联
- 汇总c#.net常用函数和方法集
- GIS C#+AE9.1叠加求交代码
- CSS里关于鼠标样式的一些属性
- 我所玩过的GALGAME——メンアットワーク!魔兽学院men at work2的中文版《永远的羁绊》全攻略
- 博客营销的十二大绝招:如何用博客营销(weamax.com)
- 技术开发频道一周精选2007-7-28
- 我们能为别人留下什么?——纪念一位真正的兄长
- web报表打印大全