search in datagridview with a textbox

1

guys I have the following problem: I should look in a datagridview column if the value of a textbox is in one of the rows of that column, if the value coinicde, I should show a message that the time is occupied, otherwise , unlocks some controls and buttons. In theory it works, but I realized that it always works with the first cell that I select, with the others it fails the validation ... I have the following code that I execute in the click event of a button:

        private void comparando()
    {
        foreach (DataGridViewRow row in dgvHorasAtencion.Rows)
        {

            string horaAtencion = Convert.ToString(row.Cells["hora"].Value);

            if (this.textBox1.Text != horaAtencion)
            {
                MessageBox.Show("UPS, LA HORA ESTA OCUPADA");
                hora = "";
                this.label18.Visible = true;
                this.label1.Visible = false;
                return;

            }
            else
            {
                MessageBox.Show("DESBLOQUEA TODO, HORA LIBRE");
                this.isNuevo = true;
                this.isEditar = false;
                this.botones();
                this.limpiarControlesInsert();
                this.habilitarControles(true);
                groupBox2.Enabled = true;
                this.label1.Visible = true;
                this.label18.Visible=false;
                return;
            }

        }
    }

Does anyone notice any failure?

Greetings to all, watch out for your comments

    
asked by Nicolas Ezequiel Almonacid 03.09.2018 в 15:49
source

1 answer

0

You have a logic problem. Having a return in the if and another in the else always comes out in the first iteration of the foreach

private void comparando()
        {
            string message = "UPS, LA HORA ESTA OCUPADA";
            bool encontrado = false;
            foreach (DataGridViewRow row in dgvHorasAtencion.Rows)
            {

                string horaAtencion = Convert.ToString(row.Cells["hora"].Value);

                if (this.textBox1.Text == horaAtencion) 
                {
                    encontrado = true;
                    message = "DESBLOQUEA TODO, HORA LIBRE";
                    break;



                }
            }


            MessageBox.Show(message);
            if (!encontrado)
            {
                hora = "";
                this.label18.Visible = true;
                this.label1.Visible = false;
            }
            else
            {
                this.isNuevo = true;
                this.isEditar = false;
                this.botones();
                this.limpiarControlesInsert();
                this.habilitarControles(true);
                groupBox2.Enabled = true;
                this.label1.Visible = true;
                this.label18.Visible = false;


            }
        }
    
answered by 03.09.2018 / 19:56
source