score:16

Accepted answer

Explicitly cast to a bool?

bool b = true;
bool? b2 = (bool?)b;

In case it's of interest, you can convert bool? to bool. You can do this by first checking HasValue which will return false if it's null or true if it is not null.

If it does have a value, you can cast to a bool.

bool? b = null;
if (b.HasValue == false) // it's null
{
  //initialize b 
  b = false;
}
else if((bool)b == true)
{
  // do whatever
}

Check out http://msdn.microsoft.com/en-us/library/bb384091.aspx for bool? to bool conversion.

score:2

dbcontext.implied = new Nullable<bool>(yes);

score:3

Can directly do something like

bool result = true;
bool? toBindData = (bool?)result;

score:0

Try Convert.ToBoolean(Your checkBox)

Below how I tested on my DataGridView to check CheckBox column and it works fine.

if ( Convert.ToBoolean( dgv.Rows[r].Cells["Delete"].Value))
{
    //Do work;
}

score:0

Here is a fix:

if (Convert.ToBoolean(chkbox.checked) == true)

Or a simpler version:

if (Convert.ToBoolean(chkbox.checked))

as the default condition anticipates the Truth of the statement.

Dependencies:

using.System;

Related Articles