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

推荐订阅源

P
Proofpoint News Feed
博客园 - 聂微东
Application and Cybersecurity Blog
Application and Cybersecurity Blog
MyScale Blog
MyScale Blog
罗磊的独立博客
H
Help Net Security
L
LangChain Blog
T
Threat Research - Cisco Blogs
量子位
S
Securelist
Last Week in AI
Last Week in AI
L
Lohrmann on Cybersecurity
T
The Exploit Database - CXSecurity.com
P
Privacy International News Feed
The Hacker News
The Hacker News
Vercel News
Vercel News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Blog of Author Tim Ferriss
T
Threatpost
Security Latest
Security Latest
P
Palo Alto Networks Blog
Microsoft Security Blog
Microsoft Security Blog
NISL@THU
NISL@THU
F
Full Disclosure
WordPress大学
WordPress大学
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Stack Overflow Blog
Stack Overflow Blog
C
Check Point Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Heimdal Security Blog
J
Java Code Geeks
Recorded Future
Recorded Future
Hugging Face - Blog
Hugging Face - Blog
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
阮一峰的网络日志
阮一峰的网络日志
U
Unit 42
B
Blog RSS Feed
月光博客
月光博客
C
Cisco Blogs
V
Visual Studio Blog
D
DataBreaches.Net
H
Hacker News: Front Page
博客园 - 叶小钗
N
News and Events Feed by Topic
爱范儿
爱范儿
A
Arctic Wolf

博客园 - 紫雨轩 .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。