Compare 2 columns in DataGridview

1

I have 2 DataGridview that load information from a database in MySQL, in the dataA I have schedules, with a column time and a data type: 09:30 ( String ) and in dataB I have the data of the appointments assigned for the day with its hour column ( String ) and the same format of the grid bull (since when booking the time, it is done with the data of that grid) .

What I have to do is a comparison between the columns hora of both grid, and in this way, block in the gridA (that of the hours) the row with the time that is already occupied (which would come being the time that appears in gridB ).

The code I use to make a comparison is the following:

//COMAPRAR SI EL CLICK VIENE DESDE EL METODO DE EDICION
if (variable == "prueba")
{
    //MessageBox.Show("DESHABILITA LA COMPARACION POR HORA");
    this.txtHorarioCita.Text = Convert.ToString(this.dgvHorarios.CurrentRow.Cells["hora"].Value);
    this.lblIDHora.Text = Convert.ToString(this.dgvHorarios.CurrentRow.Cells["idHora"].Value);

}
else
{
    this.txtHorarioCita.Text = Convert.ToString(this.dgvHorarios.CurrentRow.Cells["hora"].Value);
    this.lblIDHora.Text = Convert.ToString(this.dgvHorarios.CurrentRow.Cells["idHora"].Value);
    this.verificarHora();
}

and the verificarHora method I have it like this:

private void verificarHora()
{
    foreach (DataGridViewRow row in dgvConsultasDia.Rows) //dgvConsultasDia.Rows
    {

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

        if (horaAtencion == this.txtHorarioCita.Text)
        {
            MessageBox.Show("LA HORA SELECCIONADA ESTA OCUPADA", "CENTRO MEDICO CHILHUE", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
            hora = "";
            //this.label18.Visible = true;
            //this.label1.Visible = false;
            this.groupBox1.Enabled = false;
            this.groupBox2.Enabled = false;

            return;

        }
        else
        {
            //MessageBox.Show("la hora sel", "CENTRO MEDICO CHILHUE", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            //this.habilitarControles(true);
            //this.groupBox1.Enabled = true;
            this.groupBox2.Enabled = true;

            this.btnGuardar.Enabled = true;
            this.txtRutPaciente.Enabled = true;
            //this.label1.Visible = true;
            //this.label18.Visible = false;
            break;
        }

    }
}

The variable prueba comes from an edit button, and it works, but only once, if I repeat the process after a repeated record is found, it no longer works and it gives me the pass at insert with the existing time.

Greetings to all, I appreciate from now on any example or guidance.

    
asked by Nicolas Ezequiel Almonacid 08.09.2018 в 22:53
source

1 answer

2

I can not fully grasp what you want to do, so I'll only give you suggestions on what you can do.

1) To get values from a cell of a DGV you use the following:

dataA.Rows[index].Cells["hora"].Value.ToString()

where the index is the number of the row (You can do an iteration with a for if necessary, as I said before, I can not fully grasp what you want to do).

2) As a second recommendation I would advise a field (type bool ) name it horaOcupada in the grid that lets you know which record is "blocked" and which is not. true = occupied, false = Free

3) Having the values of both DGV you can compare them in the following way:

if (dataA.Rows[index].Cells["hora"].Value.ToString().equals(dataB.Rows[index].Cells["hora"].Value.ToString()))
dataA.Rows[index].Cells["horaOcupada"].Value = true;

If you have any questions or comments, we are ready.

    
answered by 09.09.2018 / 04:08
source