Click here to Skip to main content
15,904,153 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
for (int i = 0; i <= dgvFinControl.Rows .Count - 1; i++)
{
  if (Convert.ToBoolean (dgvFinControl.Rows[i].Cells[0].EditedFormattedValue ) == true  )
  {
    iCounter++;
  }
}
Posted
Updated 3-Aug-10 4:58am
v4
Comments
William Winner 3-Aug-10 12:50pm    
Reason for my vote of 1
You didn't ask a question.

Try
if (Convert.ToInt32(dgvFinControl.Rows[i].Cells[0].EditedFormattedValue)== Convert.ToInt32(true))

or
if (Convert.ToInt32(dgvFinControl.Rows[i].Cells[0].EditedFormattedValue)== 1)

Good luck!
 
Share this answer
 
Comments
Sandeep Mewara 3-Aug-10 12:12pm    
Reason for my vote of 3
Based on the error, it might be that the value of field is null and thus these converts would again fail!
E.F. Nijboer 3-Aug-10 18:03pm    
Correct, but the question has changed while I posted my answer. In the initial question this was Convert.ToInt32 instead of Convert.ToBoolean and therefore I simply marked the type mismatch in the condition. But still your right because then also it would have been possible for a null value but the mismatch seemed more critical at that point.
please check the dgvFinControl.Rows[i].Cells[0].EditedFormattedValue is not equal to "" and then convert to int

Thanks
Vijay r
 
Share this answer
 
Based on the error, value of:
dgvFinControl.Rows[i].Cells[0].EditedFormattedValue
is not a valid boolean value. Assuming you are expecting it to be either true or false (0 or 1), it is something else.

A simple use of DEBUGGER can tell you that. Might be its empty!

If this is the case then the answer suggested by 'E.F.Nijboer' also would not work for you. Your first check before explicit conversion should be for null/empty and then anything else.

Once you have a valid boolean data in the field you can convert it into Boolean and work.
 
Share this answer
 
First, we'd have to know what type dgvFinControl.Rows[i].Cells[0] was and what you were storing in it. There would be any number of things that you could do to avoid it.

Without knowing the type, I guess I would write
C#
for (int i = 0; i <= dgvFinControl.Rows .Count - 1; i++)
{
  bool result = false;
  try
  {
    result = Convert.ToBoolean(dgvFinControl.Rows[i].Cells[0].EditedFormattedValue);
  }catch (Exception ex)
  {
    //do something
  }
  if (result)
    iCounter++;
}
 
Share this answer
 

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



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