C#.net 下拉框combobox 代码绑定sqlserver的问题

[复制链接]
查看11 | 回复0 | 2007-8-15 16:59:15 | 显示全部楼层 |阅读模式
//combobox和dataGD的绑定数据库private void Form1_Load(object sender, System.EventArgs e){ //在formload时绑定数据表yg到comboBox1 SqlConnectionconn=new SqlConnection("server=.;uid=sa;pwd=;database=gz"); SqlDataAdapter da=new SqlDataAdapter("select * from yg",conn); DataSet ds=new DataSet(); da.Fill(ds,"yg"); this.comboBox1.DataSource=ds.Tables["yg"]; this.comboBox1.DisplayMember="ygname"; //显示的列 this.comboBox1.ValueMember="ygid";
//数据列,次列会在selectedvalue中显示}
//要在不同的方法中使用同一个数据集就要把数据集改成全局DataSet ds1=new DataSet();private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e){
SqlConnectionconn=new SqlConnection("server=.;uid=sa;pwd=;database=gz");
//根据comboBox的选择写查询语句 try {
SqlDataAdapter da=new SqlDataAdapter("select * from gz inner join yg on yg.ygid=gz.ygid where gz.ygid="+this.comboBox1.SelectedValue.ToString(),conn);
da.Fill(ds1,"gza");
this.dataGrid1.DataSource=ds1.Tables["gza"].DefaultView; } catch {}}private void dataGrid1_Click(object sender, System.EventArgs e){ //通过datagrid中选中的行号来得到dataSet中的数据 this.textBox1.Text=ds1.Tables["gza"].Rows[this.dataGrid1.CurrentRowIndex]["gzid"].ToString(); this.textBox2.Text=ds1.Tables["gza"].Rows[this.dataGrid1.CurrentRowIndex]["gznum"].ToString();}}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行