combox绑定到itemsource时如何清除组合框项目 [英] how to clear combo box items when that combox is binded to itemsource

查看:130
本文介绍了combox绑定到itemsource时如何清除组合框项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..

我在wpf应用程序中有2个组合框..我已经将它们都与database(itemsource)绑定了.现在,当我从第一组合框中选择一个项目时,我想在第二组合框中显示该特定选定项目的结果. m从第一个组合框中选择另一个项目我正在为第二个组合框中获取异常(对象引用未设置为对象的实例).是否有任何解决方案可以清除第二个组合框中... ???

i have 2 combo boxes in my wpf application.. i have binded both of them with database(itemsource). now when i select one item form 1st combox ,i want to display the result of that particular selected item in 2nd combo box.. i have tried this..but when i m selecting another item from 1st combo box i m getting exception (Object reference not set to an instance of an object) for 2nd combo box.. Is there any solution to clear 2nd combo box...???

这是我的代码...

private void Window_Loaded(object sender, RoutedEventArgs e) { BindData(); } private void BindData() { using (con = new OleDbConnection(cntstr)) { cmd = new OleDbCommand(sql2, con); OleDbDataAdapter adapter = new OleDbDataAdapter(); ds = new DataSet(); con.Open(); adapter.SelectCommand = cmd; adapter.Fill(ds, "Rack"); comboBoxRack.DataContext = ds.Tables[0].DefaultView; con.Close(); } } private void comboBoxPanel_SelectionChanged(object sender, SelectionChangedEventArgs e) { object selected = comboBoxPanel.SelectedItem; DataRow row = ((DataRowView)selected).Row;// here is the exception...Object reference not set to an instance of an object

string SelPnl =(string)row ["Panel_Id" ]; con =新的OleDbConnection(cntstr); con.Open(); OleDbCommand cmd2 =新的OleDbCommand(); cmd2.CommandText =从HubRoom_Side_Io中选择Count(Io_Number)作为CountIo,其中Panel_Id ='" + SelPnl +'"; cmd2.Connection = con; cmd2.ExecuteNonQuery(); ds2 = new DataSet(); 适配器=新的OleDbDataAdapter(); adapter.SelectCommand = cmd2; adapter.Fill(ds2); OleDbCommand cmd3 =新的OleDbCommand(); cmd3.CommandText =从面板中选择N_Port,其中Panel_Id ='" + SelPnl +'"; cmd3.Connection = con; cmd3.ExecuteNonQuery(); ds3 = new DataSet(); adapter3 =新的OleDbDataAdapter(); adapter3.SelectCommand = cmd3; adapter3.Fill(ds3); foreach(ds3.Tables [0] .Rows中的DataRow dr) { b = Convert.ToInt32(dr ["N_Port"]); Porttypetxt.Text = dr ["N_Port"].ToString(); } foreach(ds2.Tables [0] .Rows中的DataRow dr) { a = Convert.ToInt32(ds2.Tables [0] .Rows [0] ["CountIo"]); Pnllblinfo.Content = SelPnl +具有"+ a.ToString()+被占领的港口"; } } 私有无效comboBoxRack_SelectionChanged(对象发送者,SelectionChangedEventArgs e) { //comboBoxPanel.DataContext ="; 选择的对象= comboBoxRack.SelectedItem; DataRow行=((已选择(DataRowView)).Row; SelRck =(string)row ["Rack_Id"]; 字符串sql1 =从其中Rack_Id ='的面板中选择Panel_Id" + SelRck +'"; 使用(con = new OleDbConnection(cntstr)) { cmd1 =新的OleDbCommand(sql1,con); OleDbDataAdapter adapter1 =新的OleDbDataAdapter(); con.Open(); adapter1.SelectCommand = cmd1; ds1 = new DataSet(); adapter1.Fill(ds1,"Panel"); comboBoxPanel.DataContext = ds1.Tables [0] .DefaultView; con.Close(); } }

string SelPnl = (string)row["Panel_Id"]; con = new OleDbConnection(cntstr); con.Open(); OleDbCommand cmd2 = new OleDbCommand(); cmd2.CommandText = "select Count(Io_Number) as CountIo from HubRoom_Side_Io where Panel_Id='" + SelPnl + "'"; cmd2.Connection = con; cmd2.ExecuteNonQuery(); ds2 = new DataSet(); adapter = new OleDbDataAdapter(); adapter.SelectCommand = cmd2; adapter.Fill(ds2); OleDbCommand cmd3 = new OleDbCommand(); cmd3.CommandText = "select N_Port from Panel where Panel_Id='" + SelPnl + "'"; cmd3.Connection = con; cmd3.ExecuteNonQuery(); ds3 = new DataSet(); adapter3 = new OleDbDataAdapter(); adapter3.SelectCommand = cmd3; adapter3.Fill(ds3); foreach (DataRow dr in ds3.Tables[0].Rows) { b = Convert.ToInt32(dr["N_Port"]); Porttypetxt.Text = dr["N_Port"].ToString(); } foreach (DataRow dr in ds2.Tables[0].Rows) { a=Convert.ToInt32(ds2.Tables[0].Rows[0]["CountIo"]); Pnllblinfo.Content = SelPnl + " has " + a.ToString() +" Ports Occupied"; } } private void comboBoxRack_SelectionChanged(object sender, SelectionChangedEventArgs e) { //comboBoxPanel.DataContext = ""; object selected = comboBoxRack.SelectedItem; DataRow row = ((DataRowView)selected).Row; SelRck = (string)row["Rack_Id"]; string sql1 = "Select Panel_Id From Panel where Rack_Id='" + SelRck + "'"; using (con = new OleDbConnection(cntstr)) { cmd1 = new OleDbCommand(sql1, con); OleDbDataAdapter adapter1 = new OleDbDataAdapter(); con.Open(); adapter1.SelectCommand = cmd1; ds1 = new DataSet(); adapter1.Fill(ds1, "Panel"); comboBoxPanel.DataContext = ds1.Tables[0].DefaultView; con.Close(); } }

plz帮助m ....:)

plz help m....:)

谢谢...


Amit Roman

Amit Roman

推荐答案

阿米特

我不确定您如何得到此错误,但是您确实不需要修改第二个组合框数据上下文. Yuo只需更改其项源-

I'm not exactly sure how you are getting this error but you really don't need to modify the 2nd comboboxes datacontext. Yuo just need to change its itemssource -

comboBoxPanel . ItemsSource = ds1 . 表格 [ 0 ]. DefaultView ;

comboBoxPanel.ItemsSource = ds1.Tables[0].DefaultView;

,并且组合中应填充不同的项目.

如果这不能解决您的问题.您能告诉我们您的代码在哪一行上失败吗?而且您的xaml也可能有用.

谢谢.




这篇关于combox绑定到itemsource时如何清除组合框项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆