ComboBox绑定数据源时触发SelectedIndexChanged事件的处理办法

Combobox控件

改变索引触发事件SelectedIndexChanged。combobox做处理,如果引起很多控件变化,则可在该事件做处理


ComboBox最经常使用的事件就是SelectedIndexChanged。但在将ComboBox绑定到某个数据源的过程中,会触发SelectedIndexChanged
事件,而这个时候用户并没有选择内容,其SelectedValue也不是对应字段的值。那么时写在SelectedIndexChanged中的处理代码就会因为SelectedValue的内容不正确引发异常。
一般网上找到的方法是添加一个标记位,在绑定前设置为false,绑定完成后设置回true。

 
void BindComboBox()
{
    flag
=false;
    ComboxBox1.ValueMember
="ValueColumn";
    ComboxBox1.DisplayMember
="DisplayColumn";
    ComboxBox1.DataSource
=DataTable1;
    flag
=true;
}
 
private void ComboxBox1_SelectedIndexChanged(object sender, EventArgs e)

{

     if(flag)
     {
         
 //Do something
      }   
}
另外还有一种办法,就是在绑定前,将SelectedIndexChanged的委托去掉,等绑定完成后,再添加事件委托。
void BindComboBox()
        
 //去除委托
            ComboBox1.SelectedIndexChanged -= newEventHandler(ComboBox1_SelectedIndexChanged);           
            ComboBox1.DataSource
 = null;
            ComboBox1.ValueMember
 = "ValueColumn"         
            ComboBox1.DataSource
 = DataTable1;
           
 //添加委托 
            ComboBox1.SelectedIndexChanged += newEventHandler(ComboBox1_SelectedIndexChanged);
            ComboBox1.DisplayMember
 = "DisplayColumn";
}
两种方法都可以,但是之间的优劣暂时没去比较。感觉好像处理一下委托会好点。因为这种办法真的减少了事件的激发次数。
不知道还有没有其他解决方案呢?

另,贴上一段完整的代码例子。这个例子是访问SqlServer数据库的AdventureWorks,通过ProductCategory和ProductSubCategory两级目录分类去查看Product表的内容。分别使用两个ComboBox和DataGridView完成数据绑定。效果就是选择之后会联动改变相关内容。

public partial class frmProduct : Form
    {
DataSet DS
 = new DataSet();
        String ConnectionString
 = "integrated security=true; database=AdventureWorks; server=localhost; ";
       
 public frmProduct()
        {
            InitializeComponent();
        }
       
       
 private void frmProduct_Load(object sender, EventArgs e)
        {
            SqlDataAdapter da
 = new SqlDataAdapter("select ProductCategoryID,[Name] from Production.ProductCategory", ConnectionString)
;
            cbbCategories.SelectedIndexChanged
 -= newEventHandler(cbbCategories_SelectedIndexChanged);
            da.Fill(DS,
 "ProductCategory");
            cbbCategories.DataSource
 = null;
            cbbCategories.ValueMember
 = "ProductCategoryID"         
            cbbCategories.DataSource
 = DS.Tables["ProductCategory"];
            cbbCategories.SelectedIndexChanged
 += newEventHandler(cbbCategories_SelectedIndexChanged);
            cbbCategories.DisplayMember
 = "Name";//这句放在事件委托之后才会有联动效果,下同
        }

       
 private void 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
 -= newEventHandler(cbbSubCategories_SelectedIndexChanged);
            cbbSubCategories.DataSource
 = null          
            cbbSubCategories.ValueMember
 = "ProductSubCategoryID";
            cbbSubCategories.DataSource
 = DS.Tables["ProductSubCategory"];
            cbbSubCategories.SelectedIndexChanged
 += newEventHandler(cbbSubCategories_SelectedIndexChanged);
            cbbSubCategories.DisplayMember
 = "Name";
        }       

       
 private void 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"];
        }
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在easyui中,可以通过设置combobox的data属性来绑定数据源。data属性可以是一个JavaScript数组,包含要显示在combobox中的数据。 以下是一个简单的示例代码,示例中的combobox数据源是一个包含“北京”、“上海”、“广州”和“深圳”的数组: ```javascript $('#mycombobox').combobox({ data: [ {value: '北京', text: '北京'}, {value: '上海', text: '上海'}, {value: '广州', text: '广州'}, {value: '深圳', text: '深圳'} ] }); ``` 在这个示例代码中,mycomboboxcombobox的id,data属性是一个数组,包含了要显示在combobox中的数据。数组中的每个元素都是一个对象,包含了value和text属性。value属性表示该选项的值,text属性表示该选项在combobox中显示的文本。 如果需要从后台获取数据来绑定combobox,则可以使用easyui的远程数据绑定方法。在远程绑定中,需要指定combobox的url属性和valueField属性,url属性表示从后台获取数据的url,valueField属性表示数据源中每个选项的值字段。 以下是一个远程数据绑定的示例代码,示例中的combobox数据源是从远程url中获取的: ```javascript $('#mycombobox').combobox({ url: 'data.php', valueField: 'id', textField: 'text' }); ``` 在这个示例代码中,mycomboboxcombobox的id,url属性表示从后台获取数据的url,valueField属性表示数据源中每个选项的值字段,textField属性表示数据源中每个选项的文本字段。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值