ComboBox绑定数据源时触发SelectedIndexChanged事件的处理办法
2013-03-13 12:08
447 查看
转载;http://hi.baidu.com/haoyun2012/item/d780ca157a29b239b9318080 感谢原作者
ComboBox最经常使用的事件就是SelectedIndexChanged。但在将ComboBox绑定到某个数据源的过程中,会触发SelectedIndexChanged
事件,而这个时候用户并没有选择内容,其SelectedValue也不是对应字段的值。那么时写在SelectedIndexChanged中的处理代码就会因为SelectedValue的内容不正确引发异常。
一般网上找到的方法是添加一个标记位,在绑定前设置为false,绑定完成后设置回true。
绑定到ComboBoxvoid BindComboBox()
{
flag=false;
ComboxBox1.ValueMember="ValueColumn";
ComboxBox1.DisplayMember="DisplayColumn";
ComboxBox1.DataSource=DataTable1;
flag=true;
}
事件处理privatevoid ComboxBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(flag)
{
//Do something
}
}
另外还有一种办法,就是在绑定前,将SelectedIndexChanged的委托去掉,等绑定完成后,再添加事件委托。
增减委托绑定void BindComboBox()
{ //去除委托
ComboBox1.SelectedIndexChanged -=new EventHandler(ComboBox1_SelectedIndexChanged);
ComboBox1.DataSource =null;
ComboBox1.ValueMember ="ValueColumn";
ComboBox1.DataSource = DataTable1;
//添加委托
ComboBox1.SelectedIndexChanged +=new EventHandler(ComboBox1_SelectedIndexChanged);
ComboBox1.DisplayMember ="DisplayColumn";
}
两种方法都可以,但是之间的优劣暂时没去比较。感觉好像处理一下委托会好点。因为这种办法真的减少了事件的激发次数。
不知道还有没有其他解决方案呢?
另,贴上一段完整的代码例子。这个例子是访问SqlServer数据库的AdventureWorks,通过ProductCategory和ProductSubCategory两级目录分类去查看Product表的内容。分别使用两个ComboBox和DataGridView完成数据绑定。效果就是选择之后会联动改变相关内容。
二级选择框联动显示数据publicpartialclass frmProduct : Form
{
DataSet DS =new DataSet();
String ConnectionString ="integrated security=true; database=AdventureWorks; server=localhost; ";
public frmProduct()
{
InitializeComponent();
}
privatevoid frmProduct_Load(object sender, EventArgs e)
{
SqlDataAdapter da =new SqlDataAdapter("select ProductCategoryID,[Name] from Production.ProductCategory", ConnectionString)
;
cbbCategories.SelectedIndexChanged -=new EventHandler(cbbCategories_SelectedIndexChanged);
da.Fill(DS, "ProductCategory");
cbbCategories.DataSource =null;
cbbCategories.ValueMember ="ProductCategoryID";
cbbCategories.DataSource = DS.Tables["ProductCategory"];
cbbCategories.SelectedIndexChanged +=new EventHandler(cbbCategories_SelectedIndexChanged);
cbbCategories.DisplayMember ="Name";//这句放在事件委托之后才会有联动效果,下同
}
privatevoid cbbCategories_SelectedIndexChanged(object sender, EventArgs e)
{
SqlDataAdapter da =new SqlDataAdapter("select ProductSubCategoryID,[Name] from Production.ProductSubCategory where ProductCategoryID="+ cbbCategories.SelectedValue.ToString(), ConnectionString)
;
if (DS.Tables["ProductSubCategory"] !=null)
{
DS.Tables["ProductSubCategory"].Clear();
}
da.Fill(DS, "ProductSubCategory");
cbbSubCategories.SelectedIndexChanged -=new EventHandler(cbbSubCategories_SelectedIndexChanged);
cbbSubCategories.DataSource =null;
cbbSubCategories.ValueMember ="ProductSubCategoryID";
cbbSubCategories.DataSource = DS.Tables["ProductSubCategory"];
cbbSubCategories.SelectedIndexChanged +=new EventHandler(cbbSubCategories_SelectedIndexChanged);
cbbSubCategories.DisplayMember ="Name";
}
privatevoid cbbSubCategories_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbbSubCategories.SelectedIndex ==-1)
return;
SqlDataAdapter da=new SqlDataAdapter("select * from Production.Product where ProductSubCategoryID="+ cbbSubCategories.SelectedValue.ToString(), ConnectionString);
dgvProduct.DataSource =null;
if (DS.Tables["Product"] !=null)
DS.Tables["Product"].Clear();
da.Fill(DS, "Product");
dgvProduct.DataSource = DS.Tables["Product"];
}
}
ComboBox最经常使用的事件就是SelectedIndexChanged。但在将ComboBox绑定到某个数据源的过程中,会触发SelectedIndexChanged
事件,而这个时候用户并没有选择内容,其SelectedValue也不是对应字段的值。那么时写在SelectedIndexChanged中的处理代码就会因为SelectedValue的内容不正确引发异常。
一般网上找到的方法是添加一个标记位,在绑定前设置为false,绑定完成后设置回true。
绑定到ComboBoxvoid BindComboBox()
{
flag=false;
ComboxBox1.ValueMember="ValueColumn";
ComboxBox1.DisplayMember="DisplayColumn";
ComboxBox1.DataSource=DataTable1;
flag=true;
}
事件处理privatevoid ComboxBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(flag)
{
//Do something
}
}
另外还有一种办法,就是在绑定前,将SelectedIndexChanged的委托去掉,等绑定完成后,再添加事件委托。
增减委托绑定void BindComboBox()
{ //去除委托
ComboBox1.SelectedIndexChanged -=new EventHandler(ComboBox1_SelectedIndexChanged);
ComboBox1.DataSource =null;
ComboBox1.ValueMember ="ValueColumn";
ComboBox1.DataSource = DataTable1;
//添加委托
ComboBox1.SelectedIndexChanged +=new EventHandler(ComboBox1_SelectedIndexChanged);
ComboBox1.DisplayMember ="DisplayColumn";
}
两种方法都可以,但是之间的优劣暂时没去比较。感觉好像处理一下委托会好点。因为这种办法真的减少了事件的激发次数。
不知道还有没有其他解决方案呢?
另,贴上一段完整的代码例子。这个例子是访问SqlServer数据库的AdventureWorks,通过ProductCategory和ProductSubCategory两级目录分类去查看Product表的内容。分别使用两个ComboBox和DataGridView完成数据绑定。效果就是选择之后会联动改变相关内容。
二级选择框联动显示数据publicpartialclass frmProduct : Form
{
DataSet DS =new DataSet();
String ConnectionString ="integrated security=true; database=AdventureWorks; server=localhost; ";
public frmProduct()
{
InitializeComponent();
}
privatevoid frmProduct_Load(object sender, EventArgs e)
{
SqlDataAdapter da =new SqlDataAdapter("select ProductCategoryID,[Name] from Production.ProductCategory", ConnectionString)
;
cbbCategories.SelectedIndexChanged -=new EventHandler(cbbCategories_SelectedIndexChanged);
da.Fill(DS, "ProductCategory");
cbbCategories.DataSource =null;
cbbCategories.ValueMember ="ProductCategoryID";
cbbCategories.DataSource = DS.Tables["ProductCategory"];
cbbCategories.SelectedIndexChanged +=new EventHandler(cbbCategories_SelectedIndexChanged);
cbbCategories.DisplayMember ="Name";//这句放在事件委托之后才会有联动效果,下同
}
privatevoid cbbCategories_SelectedIndexChanged(object sender, EventArgs e)
{
SqlDataAdapter da =new SqlDataAdapter("select ProductSubCategoryID,[Name] from Production.ProductSubCategory where ProductCategoryID="+ cbbCategories.SelectedValue.ToString(), ConnectionString)
;
if (DS.Tables["ProductSubCategory"] !=null)
{
DS.Tables["ProductSubCategory"].Clear();
}
da.Fill(DS, "ProductSubCategory");
cbbSubCategories.SelectedIndexChanged -=new EventHandler(cbbSubCategories_SelectedIndexChanged);
cbbSubCategories.DataSource =null;
cbbSubCategories.ValueMember ="ProductSubCategoryID";
cbbSubCategories.DataSource = DS.Tables["ProductSubCategory"];
cbbSubCategories.SelectedIndexChanged +=new EventHandler(cbbSubCategories_SelectedIndexChanged);
cbbSubCategories.DisplayMember ="Name";
}
privatevoid cbbSubCategories_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbbSubCategories.SelectedIndex ==-1)
return;
SqlDataAdapter da=new SqlDataAdapter("select * from Production.Product where ProductSubCategoryID="+ cbbSubCategories.SelectedValue.ToString(), ConnectionString);
dgvProduct.DataSource =null;
if (DS.Tables["Product"] !=null)
DS.Tables["Product"].Clear();
da.Fill(DS, "Product");
dgvProduct.DataSource = DS.Tables["Product"];
}
}
相关文章推荐
- ComboBox绑定数据源时触发SelectedIndexChanged事件的处理办法
- ComboBox绑定数据源时触发SelectedIndexChanged事件的处理办法
- ComboBox绑定数据源时触发SelectedIndexChanged事件的处理办法
- ComboBox绑定数据源时触发SelectedIndexChanged事件的处理办法
- ComboBox绑定数据源时触发SelectedIndexChanged事件的处理办法
- Winform ComboBox 绑定数据源 触发SelectedIndexChanged事件 处理办法
- ComboBox绑定数据源时触发SelectedIndexChanged事件的处理办法
- WINCE中ComboBox绑定数据源时触发SelectedIndexChanged事件的处理
- C# winform ComboBox绑定数据源时触发SelectedIndexChanged事件的处理
- ComboBox使用BindingSource做数据源,更改数据源及绑定项时是否会触发SelectedIndexChanged事件?
- 绑定数据源时组合框ComboBox.DrawItem的事件处理方法
- js事件绑定不成功的处理办法
- 多个ComboBox绑定同一个数据源出现的问题解决办法
- ExtJS4 ComboBox选择第一个选项时不能触发select事件的处理
- 绑定数据源后,Repeater在创建时触发的事件,以及如果在cs文件中调用某个字段的值
- ComboBox绑定DataTable数据,用户选中项后触发事件异常,之二!
- DataGridView中对各种类型的单元格控件的事件处理(DataGridViewCheckBoxCell、DataGridViewComboBoxCell等的选定、事件触发)
- ext中处理Combobox组件点击触发后台事件的问题
- 下拉框(ComboBox)数据绑定时触发SelectedIndexChanged事件的初步解决
- DataGridView中对各种类型的单元格控件的事件处理(DataGridViewCheckBoxCell、DataGridViewComboBoxCell等的选定、事件触发)