惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
T
Threatpost
C
CERT Recently Published Vulnerability Notes
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Security Archives - TechRepublic
Security Archives - TechRepublic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
K
Kaspersky official blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Attack and Defense Labs
Attack and Defense Labs
N
News and Events Feed by Topic
Project Zero
Project Zero
H
Heimdal Security Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Know Your Adversary
Know Your Adversary
Google Online Security Blog
Google Online Security Blog
W
WeLiveSecurity
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Schneier on Security
Schneier on Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
N
News | PayPal Newsroom
Hacker News - Newest:
Hacker News - Newest: "LLM"
H
Hacker News: Front Page
L
LINUX DO - 热门话题
Spread Privacy
Spread Privacy
T
Threat Research - Cisco Blogs
Cloudbric
Cloudbric
V
Vulnerabilities – Threatpost
Hacker News: Ask HN
Hacker News: Ask HN
S
Securelist
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
TaoSecurity Blog
TaoSecurity Blog
NISL@THU
NISL@THU
N
News and Events Feed by Topic
S
Security Affairs
The Last Watchdog
The Last Watchdog
T
Tor Project blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
The Exploit Database - CXSecurity.com
Simon Willison's Weblog
Simon Willison's Weblog
P
Palo Alto Networks Blog
AWS News Blog
AWS News Blog
P
Proofpoint News Feed
C
Cisco Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
L
LINUX DO - 最新话题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Tenable Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Schneier on Security

博客园 - Charles Chen

.Net Framework 4.x 程序到底运行在哪个 CLR 版本之上(ZT) C#常用工具类——Excel操作类(ZT) C#程序以管理员权限运行(ZT) why happen "WaitHandles must be less than or equal to 64" fsn文件解析(C#) MS SQL执行大脚本文件时,提示“内存不足”的解决办法() OLDB读取excel的数据类型不匹配的解决方案(ZT) 加密配置文件(App.Config和Web.config)中connectionStrings通用方法 WinForm中使用XML文件存储用户配置及操作本地Config配置文件(zt) 理解DllImportAttribute中的属性 Winform中消息循环、异步操作、Control.Invoke&Control.BeginInvoke学习 Winform打包程序制作的快捷方式指向错误的位置(指向安装包文件) Reflector导出.NET工程项目的修复 存储过程返回值及输出参数笔记 使用本地系统帐户和域用户帐户两者区别(microsoft SQLServer2000)(ZT) SQL字符串的分组聚合(ZT) SQL中使用update inner join和delete inner join (ZT) 项目维护之WinXP IIS中HTTP500的来龙去脉 一次项目维护案例而对事务学习的笔记
C# ComboBox(DropDownList)数据绑定后,怎样再添加选项(ZT)
Charles Chen · 2011-05-25 · via 博客园 - Charles Chen

本文转载于:http://hi.baidu.com/yangyangye2008/blog/item/1775770215a22782d53f7c18.html

两者大同小异,之说comBoBox吧,我们知道数据绑定控件被绑定之后是无法再在里面添加数据的,因为这是后的DataSource是无法修改的!不管是先再comBoBox之前使用其属性Items下的Add方法添加都会被后来的DisplayMember覆盖,

①这样是不行的:

comboBox1.Items.Add("--选择所有--");

this.comboBox1.DataSource = EmployerSet;

this.comboBox1.DisplayMember = "EmployeesTable.FirstName";

这种错误时先添加一项殊不知,会被后来的覆盖掉!

②另外可能有人想的是:

this.comboBox1.DataSource = EmployerSet;

this.comboBox1.DisplayMember = "EmployeesTable.FirstName";

comboBox1.Items.Insert(0,"--选择所有--");

这一种是最普遍的错误,这时候错误会出现在红色位置:设置 DataSource 属性后无法修改项集合。

怎么解决了,在长期的经历中我发现还是有方法可行的,第二种最佳,是昨天做项目的时候发现的:

第一种方法,想修改绑定的数据源之后再绑定,代码如下:

DataTable dt = EmployerSet.Tables["EmployeesTable"];

DataRow dr = dt.NewRow();

dr["FirstName"] = "--选择所有--";

dt.Rows.InsertAt(dr, 0);

this.comboBox1.DataSource = dt;

this.comboBox1.DisplayMember = "FirstName";

方法二:

采用了数据源的管理对象BindingContext,先将当前项的值修改之后再赋予呈现成员

this.comboBox1.DataSource = EmployerSet;

DataRowView rowV = (DataRowView)this.BindingContext[EmployerSet, "EmployeesTable"].Current;

rowV["FirstName"] = "--选择所有--";

this.comboBox1.DisplayMember = "EmployeesTable.FirstName";

你可以测试发现其实 rowV["FirstName"]原本的值就是EmployeesTable.FirstName中的第一个值,只是后来被修改了,接着赋予其呈现成员,这里之所以修改数据源会成功,我自己的认为是使用到了数据的管理对象,就好像是管理员可以修改一样而别人无法修改!