dataset绑定combobox前如何添加新选项

徭谷芹sQ
2013-10-10 · TA获得超过3826个赞
知道小有建树答主
回答量:962
采纳率:0%
帮助的人:2283万
展开全部
//combobox和dataGD的绑定数据库private void Form1_Load(object sender, System.EventArgs e) { //在formload时绑定数据表yg到comboBox1 SqlConnection conn=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) { SqlConnection conn=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(); } }
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式