首页 新闻 搜索 专区 学院

关于winform的conbobox控件

0
悬赏园豆:30 [已解决问题] 解决于 2010-08-18 18:21

假如我有两个conbobox的控件,并且连接相同的数据库,我想实现一个功能当在conbobox1里选择了一个内容,那么在conbobox2里就不显示这个内容

js灰灰的主页 js灰灰 | 初学一级 | 园豆:7
提问于:2010-08-17 21:58
< >
分享
最佳答案
0

comboBox1_SelectedIndexChanged 的事件里面处理,将 conbobox2 的数据源中的 conbobox1选择的项排除掉。最好是用视图来过滤掉,而不是将数据删除,这样当conbobox1 改变选项的时候,conbobox2的数据源就不必再将之前删除的数据项再添加回来。

收获园豆:30
HUHU慈悲 | 大侠五级 |园豆:9973 | 2010-08-17 22:15
能给我给代码看看吗
js灰灰 | 园豆:7 (初学一级) | 2010-08-17 22:29
数据源: DataTable CreateDataSource() { DataTable table = new DataTable(); table.Columns.Add("ID"); table.Columns.Add("Name"); DataRow row = null; for (int i = 0; i < 10; i++) { row = table.NewRow(); row[0] = i.ToString(); row[1] = "Name" + i; table.Rows.Add(row); } return table.Copy(); } //选择事件 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { var table = this.CreateDataSource(); var view = table.DefaultView; view.RowFilter = "ID <>'" + this.comboBox1.SelectedValue + "'"; this.comboBox2.DataSource = view; } //窗体load 事件 private void Form2_Load(object sender, EventArgs e) { this.comboBox1.ValueMember = "ID"; this.comboBox1.DisplayMember = "Name"; this.comboBox2.ValueMember = "ID"; this.comboBox2.DisplayMember = "Name"; this.comboBox1.DataSource = this.CreateDataSource(); this.comboBox2.DataSource = this.CreateDataSource(); }
HUHU慈悲 | 园豆:9973 (大侠五级) | 2010-08-18 14:50
private SqlConnection myConn; private SqlDataAdapter myDataAdapter; private void Form1_Load(object sender, EventArgs e) { comboBox1.DataSource = ((DataSet)this.getDateSet()).Tables["ProductsList"]; comboBox1.DisplayMember = "product_name"; comboBox1.ValueMember = "ID"; comboBox2.DataSource = ((DataSet)this.getDateSet()).Tables["ProductsList"]; comboBox2.DisplayMember = "product_name"; comboBox2.ValueMember = "ID"; } private DataSet getDateSet() { string connStr = ConfigurationManager.ConnectionStrings["dgvConnStr"].ConnectionString; myConn = new SqlConnection(connStr); myConn.Open(); string sqlQuery = "SELECT ID, product_name from [products] ORDER BY ID"; myDataAdapter = new SqlDataAdapter(sqlQuery, myConn); DataSet ds = new DataSet(); myDataAdapter.Fill(ds, "ProductsList"); return ds; } private DataSet getDateSet(int ID) { string connStr = ConfigurationManager.ConnectionStrings["dgvConnStr"].ConnectionString; myConn = new SqlConnection(connStr); myConn.Open(); string sqlQuery = "SELECT ID, product_name from [products] WHERE ID <> " + ID + " ORDER BY ID"; myDataAdapter = new SqlDataAdapter(sqlQuery, myConn); DataSet ds = new DataSet(); myDataAdapter.Fill(ds, "ProductsList"); return ds; } private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e) { comboBox2.DataSource = ((DataSet)this.getDateSet((int)comboBox1.SelectedValue)).Tables["ProductsList"]; comboBox2.DisplayMember = "pro
js灰灰 | 园豆:7 (初学一级) | 2010-08-18 17:32
其他回答(3)
0

问题能描述再清楚点吗?比如你要什么需求,两个控件提取的SQL语句是一样的,来源于同一个表?

Astar | 园豆:40805 (高人七级) | 2010-08-18 08:20
一样的啊
支持(0) 反对(0) js灰灰 | 园豆:7 (初学一级) | 2010-08-18 17:27
@js灰灰:如果这样,可以将这两个控件绑定一个数据源DataTable,DataTable为内存变量,当第一个控件选择时,在触发的发删除中用DataTable的delete方法删除选择的一行,然后再执行第二个控件的绑定。
支持(0) 反对(0) Astar | 园豆:40805 (高人七级) | 2010-08-18 17:32
0

s绑定数据源:

DataTable DT = new DataTable();
DT.Columns.Add(
new DataColumn("id"));
DT.Columns.Add(
new DataColumn("name"));
DataRow row;
for (int i = 0; i < 3; i++)
{
row
= DT.NewRow();
row.ItemArray
= new object[] { i, i.ToString() + "选项" };

DT.Rows.Add(row);
}

this.comboBox1.DataSource = new DataView(DT);

this.comboBox2.DataSource = new DataView(DT);

控制:

if (this.comboBox1.SelectedIndex > 0)
{
DataView dv
= this.comboBox2.DataSource as DataView;

dv.RowFilter
= string.Format(" id <> {0}", this.comboBox1.SelectedValue);
}

未经测试。但是思路是这样吧。
邢少 | 园豆:10926 (专家六级) | 2010-08-18 08:42
3Q
支持(0) 反对(0) js灰灰 | 园豆:7 (初学一级) | 2010-08-18 17:33
0

同意一楼的方法

天神一 | 园豆:845 (小虾三级) | 2010-08-18 10:45
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册