Link to home
Start Free TrialLog in
Avatar of rwheeler23
rwheeler23Flag for United States of America

asked on

VS 2008 C# Input string not in correct format

Why am I receiving a message about input string not of correct format on this line?

if (Int32.Parse(dgvFlagTransactionsView[9, dgvFlagTransactionsView.CurrentCell.RowIndex].Value.ToString()) == 0)

At first I did not have the Int32.Parse method but I had to add that because I got a message about == cannot be used with object or int.

I have a cell on a datagrid that has a checkbox. What I am trying to do is change the value in the actual field. So if it value was 0 change it to 1, if its value was one change it to 0. The value is in column 10 of the grid.
private void dgvFlagTransactionsView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.ColumnIndex == 0 && e.RowIndex >= 0)
                {
                    if (Int32.Parse(dgvFlagTransactionsView[9, dgvFlagTransactionsView.CurrentCell.RowIndex].Value.ToString()) == 0)
                    {
                        dgvFlagTransactionsView[9, dgvFlagTransactionsView.CurrentCell.RowIndex].Value = 1;
                    }
                    else
                    {
                        dgvFlagTransactionsView[9, dgvFlagTransactionsView.CurrentCell.RowIndex].Value = 0;
                    }
                }
            }
            catch (Exception ex)
            {
                string eMsg = "003E: ERROR: " + ex.Message;
                if (stackTraceWanted) eMsg += "\n" + ex.StackTrace;
                MessageBox.Show(eMsg);
            }

        }

Open in new window

Avatar of rwheeler23
rwheeler23
Flag of United States of America image

ASKER

I just discovered that the value in that cell is actually of bit type.

I tried using this but VS does not like this.
if (dgvFlagTransactionsView[9, dgvFlagTransactionsView.CurrentCell.RowIndex].Value)

How do I structure an if statement with a value that already is boolean?

Avatar of Korbus
Korbus

missing an open parenthesis?
if( (Int32.Parse(dgvFlagTransactionsView[9, dgvFlagTransactionsView.CurrentCell.RowIndex].Value.ToString()) == 0)

or
if (Int32.Parse(dgvFlagTransactionsView[9, dgvFlagTransactionsView.CurrentCell.RowIndex].Value.ToString() == 0)

Open in new window

opps, ignore me pls= sorry
I got it to work doing this. Not sure if this is the correct way to do it.

if (dgvFlagTransactionsView[9, dgvFlagTransactionsView.CurrentCell.RowIndex].Value.ToString() == "False")
                    {
                        dgvFlagTransactionsView[9, dgvFlagTransactionsView.CurrentCell.RowIndex].Value = true;
                    }
                    else
                    {
                        dgvFlagTransactionsView[9, dgvFlagTransactionsView.CurrentCell.RowIndex].Value = false;
                    }
looks like a toggle, does this work?
dgvFlagTransactionsView[9, dgvFlagTransactionsView.CurrentCell.RowIndex].Value = !(dgvFlagTransactionsView[9, dgvFlagTransactionsView.CurrentCell.RowIndex].Value);

Open in new window


When you use  == true in your if statement, does that generate an error too?  (I'd think this is the same as NOT saying ==true, but i'm asking as a sanity check, because that value is what you are assigning lower down.)

This does not work. It will not even build. I get

 "Error      1      Operator '!' cannot be applied to operand of type 'object

Yes, it is simply a toggle. If it is false when clicked on change to true, if it is true when clicked on change to false.
ASKER CERTIFIED SOLUTION
Avatar of Korbus
Korbus

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial