How to pull data from a cell in the Data Grid?

Asked

Viewed 69 times

0

My database data is showing up on a DataGrid and I wanted to make them go to the TextBoxs as soon as I gave two Clicks in the specific Cell for editing, how can I do it ? Detail, the database was designed in Mysql

That’s my form code

public partial class CadastrarProdutos : Form
{
    //variaveis
    decimal Converter;
    int ativIndat;


    //conexão com o banco
    MySqlConnection conectar = new MySqlConnection("server=127.0.0.1;database=ProdPacote; Uid=root; pwd=1234;");
    public CadastrarProdutos()
    {
        InitializeComponent();
    }
    //limpando
    private void btnCancelar_Click(object sender, EventArgs e)
    {
        txtDescricao.Clear();
        txtNome.Clear();
        txtPreco.Clear();
    }

    private void selecionarCategoria()
    {
        conectar.Open();




        MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter("select * from Produto", conectar);
        DataSet DS = new DataSet();
        mySqlDataAdapter.Fill(DS);
        dataGridView1.DataSource = DS.Tables[0];
        conectar.Close();

    }



    //botao salvar
    private void btnSalvar_Click(object sender, EventArgs e)
    {
        // campos vazios
        if (txtNome.Text.Equals(""))
        {
            System.Windows.Forms.MessageBox.Show("O nome do produto está vazio, por favor digite algo");
        }
        else if (txtPreco.Text.Equals(""))
        {
            if (txtPreco.Text.Equals(""))
            {
                System.Windows.Forms.MessageBox.Show("O Preço do produto está vazio, por favor digite algo");
            }
            else
            {

            }
            //fim dps campos vazios
        }




        //eniando para o banco
        else
        {

            try
            {

                conectar.Open();
                //Convertendo

                Converter = Convert.ToDecimal(txtPreco.Text);

                //MessageBox.Show("Conectado");

                MySqlCommand Inserir = new MySqlCommand();
                Inserir.Connection = conectar;
                Inserir.CommandText = "INSERT INTO Produto (Nome, Descricao, Preco, `status`) VALUES (@peca, @nome, @quantidade, @dataentrada)";

                Inserir.Parameters.AddWithValue("@peca", txtNome.Text);
                Inserir.Parameters.AddWithValue("@nome", txtDescricao.Text);
                Inserir.Parameters.AddWithValue("@quantidade", txtPreco.Text);
                Inserir.Parameters.AddWithValue("@dataentrada", ckbAtiv.Checked);

                Inserir.ExecuteNonQuery();
                conectar.Close();
                MessageBox.Show("Cadastro Realizado!", "Concluido",
                  MessageBoxButtons.OK,
                  MessageBoxIcon.Information);
                selecionarCategoria();
            }
            catch (SqlException)
            {

                MessageBox.Show("Falha na conexao!", "falha",
                  MessageBoxButtons.OK,
                  MessageBoxIcon.Information);

            }
        }
    }
    //form
    private void CadastrarProdutos_Load(object sender, EventArgs e)
    {
        selecionarCategoria();
    }
  • Hello @Pietro. Enter the code you are using pf.

  • Hello @Joãomartins. I put the code used in the form programming

1 answer

0


Problem solved, I used the method

   private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        txtNome.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
        txtDescricao.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
        txtPreco.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();

    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.