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

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

绑定到ComboBox

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的委托去掉,等绑定完成后,再添加事件委托。

两种方法都可以,但是之间的优劣暂时没去比较。感觉好像处理一下委托会好点。因为这种办法真的减少了事件的激发次数。
不知道还有没有其他解决方案呢?

另,贴上一段完整的代码例子。这个例子是访问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 -= 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";//这句放在事件委托之后才会有联动效果,下同
} 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 -= new EventHandler(cbbSubCategories_SelectedIndexChanged);
cbbSubCategories.DataSource = null;
cbbSubCategories.ValueMember = "ProductSubCategoryID";
cbbSubCategories.DataSource = DS.Tables["ProductSubCategory"];
cbbSubCategories.SelectedIndexChanged += new EventHandler(cbbSubCategories_SelectedIndexChanged);
cbbSubCategories.DisplayMember = "Name";
} private void cbbSubCategories_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbbSubCategories.SelectedIndex == -)
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"];
}
}

参考:https://www.cnblogs.com/Bonizlee/archive/2011/05/24/2054942.html?tdsourcetag=s_pctim_aiomsg感谢楼主提供的方法

winfrom 窗体控件实现二级联动的更多相关文章

  1. 后台线程下的WinFrom窗体控件操作 Invoke

    Invoke(new MethodInvoker(delegate { ControllerLogout(controller_id, is_successful, description, cont ...

  2. OpenTK学习笔记(2)-工作窗口的三种方法创建方法(winfrom窗体控件形式创建)

    参考资料: https://social.msdn.microsoft.com/Forums/zh-TW/1b781685-c670-4338-953d-1957a8f24a66/opentkglco ...

  3. C# winform 跨线程更改窗体控件的属性

    当winform程序中新开一个线程,是无法改变主线程中窗体控件的属性的,否则运行时会报错. 若想在其他线程中控制主线程中的窗体控件,则必须利用BeginInvoke方法. 例如:添加一个名为textb ...

  4. winform窗体控件(全)

    回顾跟补充下除了昨天那常用6个其他的winform窗体控件作用 1:Button:按钮 (1)AutoSize:如果是True的情况下,内容将会撑开:False的话会另起一行 (2)Enabled: ...

  5. (转)sl简单自定义win窗体控件

    sl简单自定义win窗体控件      相信大家接触过不少win窗体控件ChildWin子窗口就的sl自带的一个  而且网上也有很多类似的控件,而今天我和大家分享下自己制作个win窗体控件,希望对初学 ...

  6. winFrom 常用控件属性及方法介绍

    目录 1.窗体(Form) 2.Label (标签)控件 3.TextBox(文本框)控件 4.RichTextBox控件 5.NumericUpDown控件 6.Button(按钮)控件 7.Gro ...

  7. 如何:对 Windows 窗体控件进行线程安全调用

    http://msdn.microsoft.com/zh-cn/library/ms171728(VS.90).aspx http://msdn.microsoft.com/zh-cn/library ...

  8. C# 静态函数调用窗体控件

    回调函数方法是静态函数,需要调用窗体控件,赋值或取值. 定义 public static Form1 mainFrm;   mainFrm = this; public partial class F ...

  9. [原创]C#按比例缩放窗体控件及字体

    按照比例缩放窗体控件及字体,如需等比例缩放,只需将x,y的比例设置成相同即可. 为了减小误差,建议使用原始尺寸来计算比例. private float X, Y; private bool b = f ...

随机推荐

  1. java 打印图形

    打印菱形 package study.stage2; /** * Created by Sandy.Liu on 2017/7/27. */public class Diamond { public ...

  2. maven 内置变量

    ${basedir} 项目根目录 ${project.build.directory} 构建目录,缺省为target ${project.build.outputDirectory} 构建过程输出目录 ...

  3. 利用django如何解析用户上传的excel文件

    https://www.jb51.net/article/119452.htm 前言 我们在工作中的时候,会有这种需求:用户上传一个格式固定excel表格到网站上,然后程序负债解析内容并进行处理.我最 ...

  4. tcpdump过滤某个端口

    一般我们使用Tcpdump时都是使用: Java代码   tcpdump -i ethx      www.2cto.com   下面这条命令就是查看80端口的访问量,进行排序,取前20位    Ja ...

  5. 代码阅读笔记:【C-COT】

    [C-COT]:Danelljan M, Robinson A, Khan F S, et al. Beyond correlation filters: Learning continuous co ...

  6. Zookeeper 配置集群环境详解

    在Linux环境下安装zookeeper 在Linux环境下安装zookeeper 1.       将zookeeper-3.4.13.tar.gz复制到linux操作系统 2.       通过p ...

  7. pyhanlp用户自定义词典添加实例说明

    pyhanlp用户自定义词典添加实例说明 pyhanlp是python版封装的的HanLP,项目地址:https://github.com/hankcs/pyhanlp 经过测试,HanLP比nltk ...

  8. 通过Hibernate API编写访问数据库的代码

    private Configuration config;// 1.声明私有配置对象类private ServiceRegistry serviceRegistry;// 2.声明私有服务注册对象类p ...

  9. 深入理解java虚拟机读后总结(个人总结记录)

    1.jvm布局:   jdk1.6版本JVM布局分为:heap(堆),method(方法区),stack(虚拟机栈),native stack(本地方法栈),程序计数器共五大区域. 其中方法区包含运行 ...

  10. ML平台_Angel参考

    Angel 是腾讯开源基于参数服务器(Parameter Server)理念的机器学习框架(为支持超大维度机器学习模型运算而生).核心设计理念围绕模型,它将高维度的大模型切分到多个参数服务器节点,并通 ...