博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ComboBox 使用数据绑定时 Sorted 属性的bug
阅读量:7244 次
发布时间:2019-06-29

本文共 1286 字,大约阅读时间需要 4 分钟。

调查一个奇怪的系统异常时发现的这个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。

转载地址:http://dmybm.baihongyu.com/

你可能感兴趣的文章
UITextField,UITextView字数限制
查看>>
Spring 循环依赖
查看>>
sencha touch 在线实战培训 第一期 第二节
查看>>
Mirror--使用证书配置镜像模板
查看>>
Caused by: java.lang.OutOfMemoryError: PermGen space
查看>>
Step by Step 設定 TFS 2012 Create Team Project 權限 - 避免 TF218017、TF250044
查看>>
ArcGIS 10 安装程序及破解文件
查看>>
C#中读写JSON风格的配置信息
查看>>
Spring-Context之一:一个简单的例子
查看>>
(转)S5PV210 三个Camera Interface/CAMIF/FIMC的区别
查看>>
(转)x264重要结构体详细说明(1): x264_param_t
查看>>
struct和typedef struct
查看>>
9.5 在 C# 中使用 F# 库
查看>>
2016第6周六
查看>>
Windows 下 绿化 Oracle
查看>>
利用京东云擎架设免费Wordpress 博客(git方式)
查看>>
Linux开发环境搭建与使用——ubuntu更新设置
查看>>
POJ 3740 Dancing Links
查看>>
iOS开发--使用NSMutableAttributedString 实现富文本
查看>>
十一、jdk命令之Jstatd命令(Java Statistics Monitoring Daemon)
查看>>