ComboBox绑定DataTable数据,用户选中项后触发事件异常。

 

------------

 

下文来自CSDN博客,转载请标明出处:http://blog.csdn.net/gaofeng2000/archive/2009/05/06/4156247.aspx

 

ComboBox的SelectionChangeCommitted事件


asp.net下,dropdownlist控件有个selectedindexchanged事件,这个常用来做联动。

这几天做一个windows程序,用到ComboBox控件,根据以前的经验,就想当然的激活了selectedindexchanged事件,但是,错误马上就跳了出来,web下的经验在这里不灵了。折腾了老半天,看完电视还在想着这事儿,却总是搞不定,无意中在网上搜到一篇博文,和我的问题一样,这才把压在心头的这块石头挪开。

我这里绑定的数据源是数据库的一个查询,需要实现员工信息随部门信息变化的联动。如果采用默认的事件,也就是双击控件时自动注册的那个事件,对于普通的数据应该没啥问题,但如果数据来源于数据库,初始化控件时,会触发一次该事件,但因为尚未成功绑定,所以还无法正常获取部门id。在该事件的下面有个叫做SelectionChangeCommitted的事件,它会在选择完成后触发,在此事件下添加相应的代码,错误就不见了。

 

-------------

 

下文来自:http://www.cnblogs.com/yf611x/archive/2005/08/23/221391.html

ComboBox控件的SelectedIndexChanged和SelectionChangeCommitted事件

我在使用VS.NET 2003编写Windows Forms应用程序时,需要在表单上放置ComboBox控件并绑定DataSet填充数据,用作查询过滤器:

comboBox1.DataSource  =  dataSet1.Tables[ 0 ];
comboBox1.DisplayMember 
=   " Name " ;
comboBox1.ValueMember 
=   " Value " ;

我双击该控件编写事件代码,VS.NET自动生成了 comboBox1_SelectedIndexChanged事件,我的本意是表单上的DataGrid控件中的数据按用户选择的值进行过滤:

FillData(MessageBox.Show(comboBox1.SelectedValue.ToString());

但是 .DataSource.DisplayMember操作都会触发 SelectedIndexChanged事件,而这时comboBox1.SelectedValue.ToString()返回的值是"System.Data.DataRowView"。

解决的办法是使用SelectionChangeCommitted事件,在MSDN帮助文档里是这样解释的:

SelectionChangeCommitted: You can create a SelectionChangeCommitted event handler to provide special handling for the ComboBox when the user changes the selected item in the list.

SelectedIndexChanged: You can create an event handler for this event to determine when the selected index in the ComboBox has been changed. This can be useful when you need to display information in other controls based on the current selection in the ComboBox. You can use the event handler for this event to load the information in the other controls.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值