Datagridview cells values not equal but they should be c#

616 Views Asked by At

I have a data bound datagridview table where the user can type in a set of values in each row. There is an associated CellContentClick event that gets called each time the user changes/clicks on a new cell. In that event, it first checks that the row is full, then there is an if statement that is passed checking and whether or not two of the cells of interest are equal in value.

When the user clicks on a new cell, it only runs the case that the two cells are not equal, both when they are equal and when they are not. I've tried switching it the other way to say if it is equal == run the if statement, but it does not pass through the statement. I've also tried an if/else if statement to force the 2 cases, but it still did not pass through the case of them being equal. Why is it not passing through the 'is equal to'/'else' statement when it shows in the messagebox that the values should theoretically be equal?

This is the code:

'''private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e) { //MessageBox.Show("The cells content clicked"); //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ MessageBox.Show("The cells changed"); int empty_row = 0;

        if (dataGridView1.RowCount != 0)
        { // If there is more than 1 row, check respective columns

            for (int i = 0; i < Convert.ToInt32(number_of_sections.Text); i++)
            {
                // Check that the row is not empty
                //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                for (int j = 0; j <= dataGridView1.Columns.Count - 1; j++)
                {
                    if (dataGridView1.Rows[i].Cells[j].Value == null || dataGridView1.Rows[i].Cells[j].Value == DBNull.Value || String.IsNullOrWhiteSpace(dataGridView1.Rows[i].Cells[j].Value.ToString()))
                    {
                        empty_row = 1; // 1 when there is an empty cell
                    }
                    else
                    {
                        // if the cell has a value in it, do nothing
                    }

                    //MessageBox.Show("this is the cell " + "'" + dataGridView1.Rows[0].Cells[i].Value + "'");
                }
                if (empty_row == 0)
                {
                    // if the cell has a value in it, compare the diameters to determine profile
                    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                    if (dataGridView1.Rows[i].Cells[3].Value != dataGridView1.Rows[i].Cells[4].Value)
                    {
                        dataGridView1.Rows[i].Cells[2].Value = "linear";
                        MessageBox.Show("Passed through linear if, the value is " + dataGridView1.Rows[i].Cells[3].Value + " and " + dataGridView1.Rows[i].Cells[4].Value);
                    }
                    else 
                    {
                        dataGridView1.Rows[i].Cells[2].Value = "flat";
                    }

                    // if the cell has a value in it, compare the speeds to determine profile
                    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                    if (dataGridView1.Rows[i].Cells[6].Value != dataGridView1.Rows[i].Cells[7].Value)
                    {
                        dataGridView1.Rows[i].Cells[5].Value = "linear";
                        MessageBox.Show("Passed through linear if, the value is " + dataGridView1.Rows[i].Cells[6].Value  + " and " + dataGridView1.Rows[i].Cells[7].Value);
                    }
                    else 
                    {
                        dataGridView1.Rows[i].Cells[5].Value = "flat";
                    }
                }
                else
                {
                    empty_row = 1; // 1 when there is an empty cell
                }
                empty_row = 0; // reset
            }
        }
    } '''

the cells are different values, it passes through the case in which they're different values

the cells are the same value, but it passes through the case in which they're not

1

There are 1 best solutions below

0
Rachael Sarah On

@JohnG answered the question in the comment above, and it solved my problem,

"I am pretty sure the “if” statement… if (dataGridView1.Rows[i].Cells[3].Value != dataGridView1.Rows[i].Cells[4].Value) {…} will ALWAYS return true. dataGridView1.Rows[i].Cells[3].Value is going to return an Object, as will dataGridView1.Rows[i].Cells[4].Value … which obviously will NOT be the same objects. I am confident you need to convert those objects to ints and THEN compare them. In this case, since the comparison is “equals” then you could compare the strings, however if you need “<” or “>”… then you need to convert the values to ints."