How to read only a record of a DataGridView that has information from the BD? C #

0

My problem is that I would like to read the complete record of a table in my database, well, so far, it brings me the information and shows it in the form that I should, because what I want to do is an update, only that I read all the records and the only one that shows me is the last record of that table.

        while (reader.Read())
        {

            txtArticulo.Text = reader.GetString("Articulo");
            txtArticulo.ForeColor = Color.Black;
            txaDescripcion.Text = reader.GetString("Descripcion");
            txaDescripcion.ForeColor = Color.Black;
            txtCosto.Text = reader.GetString("Costo");
            txtCosto.ForeColor = Color.Black;
            spImportancia.Value = Convert.ToDecimal(reader.GetString("Importancia"));

            if (reader.GetString("Obligatorio") == "no")
            {
                rbNo.Checked = true;

            }
            else if (reader.GetString("Obligatorio") == "si")
            {

                rbSi.Checked = true;

            }

        }

This reads all the records, what I want to do is just read the record that is selected in the table and fill it out on my form.

As you can see, select the second record, but always record the last one.

    
asked by David 20.01.2017 в 00:31
source

1 answer

1

In the event of your Datagrid

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        string id = dataGridView1.CurrentRow.Cells[0].Value.ToString();
    }

And in your query:

  string consulta_p = "SELECT * FROM tutabla WHERE id='" + id + "'";
  MySqlCommand Orden = new MySqlCommand(consulta_p,conexion);
  MySqlDataReader Lector = Orden.ExecuteReader();
   if (Lector.Read())
   {
    //El dato existe
    TextBox.Text1=lector["campo"].ToString());
   }else{
MessageBox.Show("NO EXISTE", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

  }
    
answered by 20.01.2017 / 00:49
source