手记

可空的布尔值怎样绑定

在数据库中,存储布尔值的字段是允许可空的。如果空值绑定至DropDownList控件上时,会出现异常:


上例中,并没有真正去读取数,只是获取数据时,有获取到null值。 你可以根据这篇作为实例样版程序:
http://www.cnblogs.com/insus/archive/2013/01/28/2880656.html

 

解决方法,就是在获取的值判断一下,是否为空值:

View Code

 private void Data_Binding()    {        DataTable dt = DataSource("Whether.xml");        object v = null;  //dataTable["v"];  用户选定存储在数据库的值。        if (v == null)  //(v == DBNull.Value)            obj.DropDownListParse(this.DropDownListBooleanDemo, dt, "WhetherName", "WhetherId", string.Empty);        else            obj.DropDownListParse(this.DropDownListBooleanDemo, dt, "WhetherName", "WhetherId", v.ToString(), string.Empty);    }


当值为空时,运行效果:

 

 

0人推荐
随时随地看视频
慕课网APP