我如何...使用复选框将布尔值表示为“是/否”而不是真/假? [英] How do I...Make A Boolean Be Represented As 'yes / No' Instead Of True/False with Checkbox?

查看:124
本文介绍了我如何...使用复选框将布尔值表示为“是/否”而不是真/假?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有一个复选框用于保存发票状态(是 - 否)

I have a checkbox in my project to save invoice status (Yes-No)

If ch1.Checked = True Then cmd.Parameters.AddWithValue("@cancelled", "Yes") Else cmd.Parameters.AddWithValue("@cancelled", "No")



保存和显示正常工作,但当我尝试读取带有datareader的列如下时


Saving and displaying is working normally, but when I try to read the column with datareader as the following

<pre lang="vb">
ch1.Checked = dr!cancelled
<pre lang="vb">





它给出我的错误



[FormatException:输入字符串的格式不正确。]



[InvalidCastException:从字符串No到Boolean类型的转换无效。]



it gives me error

[FormatException: Input string was not in a correct format.]

[InvalidCastException: Conversion from string "No" to type 'Boolean' is not valid.]

推荐答案

与异常消息建议一样,您正在尝试执行无效的转换。 />


在我看来,在数据库中,状态应该保存布尔值,根据需要可以格式化为是或否。



如果你想保留其他所有内容,你必须检查取消的值,并将相应的布尔值分配给 ch1.Checked 。这样的事情:



Like the Exception message suggests, you are trying to perform an invalid casting.

In my opinion, in database, the status should save the boolean values which can be formatted as "Yes" or "No" according to the needs.

If you want to keep everything else as is, you have to check the value of cancelled and assign the respective boolean value to ch1.Checked. Something like this:

ch1.Checked = dr!cancelled == "Yes"; //Returns true if "Yes", false otherwise.





这只是一个例子。您可以使用System.String中的方法来比较值,显然,将字符串设置为大写或小写,然后执行比较。



This is just an example. You can use Methods from the System.String to compare the values and obviously, make the string to either Upper or to Lower case and then perform the comparison.


这篇关于我如何...使用复选框将布尔值表示为“是/否”而不是真/假?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆