mardi 9 novembre 2021

nested if statement runs correctly without else statement, but when the else statement is added it skips my if

I am making up some code to select a row in a datagridview based on input of a textbox. works great without the else statement attached. but once the else statement is added on it isnt reading my if or just skipping it. i can type in the same number with and without the else statement and it will succeed or fail based on if the else statement is there. just not too sure where im going wrong.

 private void transactionSerial_Box_TextChanged(object sender, EventArgs e)
    {
        string serialSearch = transactionSerial_Box.Text;
        int rowIndex = -1;

        if (transactionSerial_Box.Text.Length == 4)
        {
            DataRow matchingSerials = tooldataSet.Tool.Rows.Find(serialSearch);
           


            foreach (DataGridViewRow row in toolDataGridView.Rows)
            {
                if (row.Cells[0].Value.ToString().Equals(serialSearch))
                {
                    toolDataGridView.ClearSelection();
                    rowIndex = row.Index;
                    toolDataGridView.Rows[rowIndex].Selected = true;
                    break;
                }
                else
                {
                    MessageBox.Show("Serial number '" + serialSearch + "' is not a valid serial number. Please try again.", "invalid serial number", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    transactionSerial_Box.Text = "";
                    rowIndex = 0;
                    toolDataGridView.Rows[rowIndex].Selected = false;
                    break;
                }
               
            }
            
        }

Aucun commentaire:

Enregistrer un commentaire