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 ...
随机推荐
- VNC Viewer连接打开remote display的VMware虚拟机出现闪退
只需修改vnc option里面Advanced-->expert-->ColourLevel的值为“rgb222” or “full”即可. 说明:rgb111--8 colours,r ...
- 运用MQTT-JMeter插件测试MQTT服务器性能
今天我们介绍XMeter团队带来的新版MQTT-JMeter插件,您可以更为方便地添加MQTT连接.发布.订阅取样器,构造组合的应用场景,例如背景连接.多发少收.少发多收,计算消息转发时延等.利用该插 ...
- Android 单元测试实践
1. 在Android Studio创建工程 工程名称 AndroidUnitTest 2. 创建测试类CommonUtil 在类中加入sum和multiply方法 在CommonUtil类中右击 ...
- [转]You Could Become an AI Master Before You Know It. Here’s How.
转自:https://www.technologyreview.com/s/608921/ai-algorithms-are-starting-to-teach-ai-algorithms/# You ...
- Win10+Ubuntu1604双系统
原本电脑有一块固态硬盘和机械硬盘,用来跑win10的,现在想直接在ubuntu上跑tensorflow,所以加了块320G的机械硬盘单独跑ubuntu. 一.准备 1.ubuntu-16.04.3-d ...
- 阅读OReilly.Web.Scraping.with.Python.2015.6笔记---找出网页中所有的href
阅读OReilly.Web.Scraping.with.Python.2015.6笔记---找出网页中所有的href 1.查找以<a>开头的所有文本,然后判断href是否在<a> ...
- django 初始化 介绍 生命周期
安装好django,配置模板,静态文件 # 创建Django工程 django-admin startproject [工程名称] mysite - mysite # 对整个程序进行配置 - init ...
- How to set up github to work with Visual Studio 2013
http://michaelcrump.net/setting-up-github-to-work-with-visual-studio-2013-step-by-step/ 1. Create gi ...
- svn hooks post-commit钩子自动部署
#!/bin/sh #修改为服务编码 export LANG=zh_CN.utf- #Set variable REPOS="$1" REV="$2" SVN= ...
- 黄聪:移动应用抓包调试利器Charles
一.Charles是什么? Charles是在 Mac或Windows下常用的http协议网络包截取工具,是一款屌的不行的抓包工具,在平常的测试与调式过程中,掌握此工具就基本可以不用其他抓包工具了 ...