winfrom 窗体控件实现二级联动
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 窗体控件实现二级联动的更多相关文章
- 后台线程下的WinFrom窗体控件操作 Invoke
Invoke(new MethodInvoker(delegate { ControllerLogout(controller_id, is_successful, description, cont ...
- OpenTK学习笔记(2)-工作窗口的三种方法创建方法(winfrom窗体控件形式创建)
参考资料: https://social.msdn.microsoft.com/Forums/zh-TW/1b781685-c670-4338-953d-1957a8f24a66/opentkglco ...
- C# winform 跨线程更改窗体控件的属性
当winform程序中新开一个线程,是无法改变主线程中窗体控件的属性的,否则运行时会报错. 若想在其他线程中控制主线程中的窗体控件,则必须利用BeginInvoke方法. 例如:添加一个名为textb ...
- winform窗体控件(全)
回顾跟补充下除了昨天那常用6个其他的winform窗体控件作用 1:Button:按钮 (1)AutoSize:如果是True的情况下,内容将会撑开:False的话会另起一行 (2)Enabled: ...
- (转)sl简单自定义win窗体控件
sl简单自定义win窗体控件 相信大家接触过不少win窗体控件ChildWin子窗口就的sl自带的一个 而且网上也有很多类似的控件,而今天我和大家分享下自己制作个win窗体控件,希望对初学 ...
- winFrom 常用控件属性及方法介绍
目录 1.窗体(Form) 2.Label (标签)控件 3.TextBox(文本框)控件 4.RichTextBox控件 5.NumericUpDown控件 6.Button(按钮)控件 7.Gro ...
- 如何:对 Windows 窗体控件进行线程安全调用
http://msdn.microsoft.com/zh-cn/library/ms171728(VS.90).aspx http://msdn.microsoft.com/zh-cn/library ...
- C# 静态函数调用窗体控件
回调函数方法是静态函数,需要调用窗体控件,赋值或取值. 定义 public static Form1 mainFrm; mainFrm = this; public partial class F ...
- [原创]C#按比例缩放窗体控件及字体
按照比例缩放窗体控件及字体,如需等比例缩放,只需将x,y的比例设置成相同即可. 为了减小误差,建议使用原始尺寸来计算比例. private float X, Y; private bool b = f ...
随机推荐
- 我的虚拟机静态IP配置
- 为什么 PCB 生产时推荐出 Gerber 给工厂?
为什么 PCB 生产时推荐出 Gerber 给工厂? 事情是这样的,有一天电工王工,画了一块 PCB,发给 PCB 板厂. 过了几天 PCB 回来了,一看不对呀,这里的丝印怎么少了,那里怎么多了几条线 ...
- linux 自总结常用命令(centos系统)
查看apache2的命令 httpd -V 其中HTTPD_ROOT和SERVER_CONFIG_FILE 就可以确定httpd.conf(Apache配置文件)的路径了 apache启动.停止.重 ...
- SpringBoot入门学习笔记
SpringBoot是SpringMVC的升级版,SpringBoot的特点: application.properties文件配置: server.port = 8080端口配置 server.co ...
- cpu怎么实现运算的
这么好玩的问题,没人回答说不过去.不懂电子元件的码农我要强答一发了.太学术的我不会,你们也听不懂.所以臭不要脸非常不严谨地科普一记.说错不许打我.另外关于这个问题,推荐<编码 (豆瓣)> ...
- 基于tornado的文件上传demo
这里,web框架是tornado的4.0版本,文件上传组件,是用的bootstrap-fileinput. 这个小demo,是给合作伙伴提供的,模拟APP上摄像头拍照,上传给后台服务进行图像识别用,识 ...
- PHP中的插件机制原理和实例
PHP项目中很多用到插件的地方,更尤其是基础程序写成之后很多功能由第三方完善开发的时候,更能用到插件机制,现在说一下插件的实现.特点是无论你是否激活,都不影响主程序的运行,即使是删除也不会影响. 从一 ...
- CPS(Cyber-Physical Systems)白皮书-摘选
<中国制造2025>提出,“基于信息物理系统的智能装备.智能工厂等智能制造正在引领制造方式变革”,要围绕控制系统.工业软件.工业网络.工业云服务和工业大数据平台等,加强信息物理系统的研发与 ...
- Ubuntu 14.04 下安装 MongoDB 服务器 和 PHP MongoDB Driver 数据驱动
https://laravel-china.org/topics/309/install-mongodb-server-and-php-mongodb-driver-data-driver-under ...
- tomcat源码 Container
1.Container的有四个子容器,分别是Engine,Host,Context,Wrapper,如下: 1.Engine:整个Catalina servlet引擎,标准实现为StandardEng ...