在网上看到:Dropdownlist中 DataTextFormatString = "{0:D2}" 不起作用,网址为:http://bytes.com/forum/thread49149.html,恰巧,今天我也碰到了这个问题,因此顺便把我的解决方法写了出来。
I want to populate a dropdownlist from an array of integers
of two digit maximum.
suppose we have these numbers 1, 2 , 15 ,....
i want the numbers to be displayed in this format:
01,02,15....
so I was using the DataTextFormatString property, but I couldn't reach
the desirable result.
出现这个问题的原因就是DropDwonlist的DataTextField显示的字段不是int 类型,因为要使用{0:D2}这种格式的时候,必须是整数类型,如下面代码一定会起作用:
DropDownYgid.DataTextFormatString = "{0:D2}";
DropDownYgid.DataTextField = "id"; //必须保证id的类型为int
DropDownYgid.DataSource = dt.DefaultView;
DropDownYgid.DataBind();