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

推荐订阅源

Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
N
Netflix TechBlog - Medium
腾讯CDC
C
Check Point Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
S
SegmentFault 最新的问题
F
Fortinet All Blogs
美团技术团队
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
F
Full Disclosure
Recorded Future
Recorded Future
D
DataBreaches.Net
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
J
Java Code Geeks
I
InfoQ
Y
Y Combinator Blog
A
About on SuperTechFans
AI
AI
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
W
WeLiveSecurity
M
MIT News - Artificial intelligence
雷峰网
雷峰网
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Docker
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
V2EX - 技术
V2EX - 技术
Project Zero
Project Zero

博客园 - 紫雨轩 .Net

DNGuard Enterprise v3.56 released DNGuard Enterprise v3.30 released DNGuard Enterprise v3.2 released DNGuard 企业版 v3.1 发布 DNGuard 专业版 v2.95 发布 DNGuard Enterprise v2.95 released 在 FlexGrid 控件中指定最右侧显示的列 .Net 中枚举AppDomains Windows 2003 上使用 Windows Live Writer .Net程序集的不同加载方式,以及其在内存中格式 DNGuard Enterprise v2.92 released - 紫雨轩 .Net DNGuard 标准版 v2.90发布 DNGuard Enterprise v2.90 released DNGuard Enterprise v2.82 released 采用Native 引导方式的.Net加密保护 C#中使用晚绑定实现压缩Access数据库 直接在.Net程序(C#)中执行 native code C#复杂表达式的问题 DNGuard HVM Trial V2.82 发布
ComboBox 使用数据绑定时 Sorted 属性的bug
紫雨轩 .Net · 2009-06-25 · via 博客园 - 紫雨轩 .Net

调查一个奇怪的系统异常时发现的这个bug,现象就是用户在ComboBox里面选择一项后,程序在SelectIndexChanged 事件中根据用户选择刷新数据时出现错误。跟踪调试后才发现原来是SelectedValue 值和用户选择的Item项不一致。

但是为什么会出现这样奇怪的错误呢?
经过调查发现是因为设置了ComboBox的Sorted属性引起的。

重新的测试代码如下:

 1 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
 2         {
 3             DataRowView drv = comboBox1.SelectedItem as DataRowView;
 4             label1.Text = string.Format("Value: {0} == {1} ?", comboBox1.SelectedValue, drv.Row["ID"]);
 5         }
 6 
 7         private void frmTest_Load(object sender, EventArgs e)
 8         {
 9             DataTable dt = new DataTable();
10             dt.Columns.Add("ID"typeof(int));
11             dt.Columns.Add("Name"typeof(string));
12             dt.Rows.Add(1"f - 1");
13             dt.Rows.Add(2"e - 2");
14             dt.Rows.Add(3"d - 3");
15             dt.Rows.Add(4"c - 4");
16             dt.Rows.Add(5"b - 5");
17             dt.Rows.Add(6"a - 6");
18             
19             comboBox1.DataSource = dt;
20             comboBox1.ValueMember = "ID";
21             comboBox1.DisplayMember = "Name";            
22         }

在ComboBox里面选择 a -6 的话,SelectedValue 是 1 。

实际上因为设置了Sorted属性为true。 a - 6 显示在第一个了,也就是说 SelectedIndex 是 1.

通过测试发现, SelectedValue的值 是按照原始DataTable中Row的顺序获取的。

但是通过 SelectedItem 获取的 ID 值是 6 ,这是正确的。所以SelectedItem的赋值还是正确的。

估计这应该是微软的一个bug,但奇怪的是为什么没有两个一起错,而是一个错一个对。

实验环境:

VS2005 Vista .Net Framework 3.5 SP1 v2.0.50727.3074 。

有其它环境的朋友可以试试在其它版本的框架中是否也有同样的bug。