Click here to Skip to main content
15,400,333 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am creating windows application using datagridview and chkbox, I am getting error from cell values calculation time.

C#
rc = Convert.ToBoolean(frm.checkBox17.Checked = (bool)dataGridViewX1.CurrentRow.Cells[22].Value);


How to solve above error any one give me some ideas.

What I have tried:

System.InvalidCastException: 'Specified cast is not valid.'
Posted
Updated 22-Jun-22 7:47am

1 solution

That's an assignment, not a comparison - so the result that you are converting with ToBoolean is probably not what you expected. Probably, you meant "==" rather than "=".

But the problem is almost certainly the cast of the Value property to a bool - use the debugger to find out exactly what the cell contains, and then start look for what it should be. Chances are you are looking at the wrong column, or a empty row at the bottom of the DGV.

And your shouldn't really be using the DGV itself, but the DataSource from which it was filled; and not using "magic numbers" like "22" anyway.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900