I'm having a problem with my .net application where I'm trying to take a value from a DataTable cell and cast it to a bool.
The DataTable is bound to a DataGridView and the cell is a CheckBox.
When I check if that cell is null, it returns as not null. However when I try to get the value, it returns nothing and will not cast it to a bool.
Any help appreciated.
private void dgvItems_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 2 || e.ColumnIndex == 3)
{
var test = dtItems.Rows[e.RowIndex][e.ColumnIndex];
if (test != null)
{
if ((bool)test)
{
MessageBox.Show("true");
}
else
{
MessageBox.Show("false");
}
}
else
{
MessageBox.Show("null");
}
}
}
Exception thrown at (bool)test:
System.InvalidCastException: 'Specified cast is not valid.'
