Click here to Skip to main content
15,920,708 members

Comments by Member 15154225 (Top 6 by date)

Member 15154225 28-Jul-22 15:00pm View    
The 'data type' is 'bit' for this column in SSMS. In VS, it shows 'System.Boolean'. I have the following code:

foreach(DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[6].Value != null)
{
if ((bool)row.Cells[6].Value)
{
row.Cells[3].Value = null;
}
}
}

When I execute, it returns the error:

- $exception {"Specified cast is not valid."} System.InvalidCastException

Is it a problem that the majority of these rows are NULL and not 0 or 1?
Member 15154225 28-Jul-22 14:47pm View    
It looks like it's classified as 'bit'.
Member 15154225 28-Jul-22 14:42pm View    
I'm not sure what the problem is. Now that I've modified my code based on your latest response, I'm receiving the error:

- $exception {"Specified cast is not valid."} System.InvalidCastException
Member 15154225 28-Jul-22 14:12pm View    
I am still experiencing the same issue with the code you outline above. Say there are 150 rows returned and I check the bool (column 6) in row 51, it turns all rows in column 3 blank (null) when I'm only aiming to nullify column 3 for row 51.
Member 15154225 28-Jul-22 13:52pm View    
Sorry, what I meant is that when I started, I knew that the columns were addressed beginning with zero, as I addressed them in a separate piece of code, as such, but mistakenly started at one later on in my code. Thanks for pointing this out.

I'm not sure how to write it without the snippet of code you reference. I've also tried what is shown below, but when I check a single cell in column six, every cell in column three goes null. I'm trying to keep it to the respective row.

for (int i = 0; i < dataGridView1.RowCount - 1; i++)
{
if (dataGridView1.Rows[i].Cells[6].Value != null)
{
dataGridView1.Rows[i].Cells[3].Value = null;
}
}