------------
下文来自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事件
comboBox1.DisplayMember = " Name " ;
comboBox1.ValueMember = " Value " ;
我双击该控件编写事件代码,VS.NET自动生成了 comboBox1_SelectedIndexChanged事件,我的本意是表单上的DataGrid控件中的数据按用户选择的值进行过滤:
但是 .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.