简体   繁体   中英

Conversion of Boolean value into String value in C#

I have a boolean field in a MySQL database. When displaying the selected rows from the DB on the datagrid in C#, I want this field to be displayed as "true" or "false".

Can somebody help me out in telling how can I do it?

Typically, this will happen automatically. It depends on how you're pulling the Boolean across into C#, but it will normally get treated as a bool , which in turn will turn into "True" or "False" when it's ToString() method is called.

String.Format("The boolean value is {0}", boolValue ? "true" : "false");

You could wrap the ternary statement in some ToFriendlyString() extension method. This will allow you to say ANYTHING; true/false, yes/no, up/down, black/white, whatever the Boolean value really represents in your model.

Boolean.ToString() returns a capitalized "True" or "False"; you can format that as necessary using ToLower() .

The above answers will work for you in C#, however if you can do it at the Database level:

CASE WHEN FIELD_NAME 1 THEN 'TRUE' ELSE 'FALSE' END AS [FIELD NAME]

This would require you to change the return type to a string/varchar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM