如何combobox 数据绑定??
答案:2 悬赏:30
解决时间 2021-02-18 15:03
- 提问者网友:余味
- 2021-02-18 04:40
combobox 数据绑定
最佳答案
- 二级知识专家网友:星痕之殇
- 2021-02-18 05:12
//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();
}
}
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();
}
}
全部回答
- 1楼网友:余生继续浪
- 2021-02-18 06:30
是b/s 的还是c/s的,如果是b/s的有两种方法: 一:拖一个sqldatasource数据源控件(如果是连接sqlserver数据库的话) 在里面新建数据源,有向导提示,一步一步的跟着向导走,左后新建好数据源就把数据源绑定在combobox上,右键点击combobox有一个选择数据源选项,直接把刚才配好的数据源选上就ok,也可以手写代码:(我只是举个例子,具体情况你对号入座) public datatable getdtable() { try { string strsql = string.format("select * from student"); if (sqlcon != null) { studtable = new datatable(); sqldataadapter sqladapter = new sqldataadapter(strsql, sqlcon); sqladapter.fill(studtable); } } catch (sqlexception on) { on.tostring(); return null; } return studtable; } 写一个方法返回datatable对象 然后就sqldatasource1.datasource=studtable; this.databind(); c/s的和这差不多 你如果选择用数据源控件的话就要注意你用的是什么数据库就选择相应的数据源控件
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯