Double click on the row of a DataGridView transports the data to a form

2

Good morning, everyone.

Next, I have a form (query) that contains the DataGridView. And the registration form in another form.

I would like to understand, how can I double-click a row in the DataGridView to send all the data in the row to this registration form (so I can make the crud to change).

Image to illustrate:

Datagridclass

classdatagride{bancoconexao=newbanco();//AtributosprivatestringnomeTabela;privateDataGridViewnomeDataGride;//Construtorpublicdatagride(stringpnt,DataGridViewpndg){nomeTabela=pnt;nomeDataGride=pndg;}publicdatagride(DataGridViewpndg){nomeDataGride=pndg;}//MétodopublicDataGridViewcarregarGride(stringsql="")
    {

        DataSet ds = new DataSet();
        DataTable dt = new DataTable(); //Nova tabela

        ds.Tables.Add(dt);

        NpgsqlDataAdapter da = new NpgsqlDataAdapter();

        if (sql == "")
        {
            sql = "select * from " + nomeTabela;
        }

        da = new NpgsqlDataAdapter(sql, conexao.conecta());
        da.Fill(dt);

        nomeDataGride.DataSource = dt.DefaultView;

        conexao.desconecta();
        return nomeDataGride;
    }

** Builder **

public cliente(cliente c, string pn, string pe, int pt, string pem, string ps, int pcod)
    {
        nome = pn;
        endereco = pe;
        telefone = pt;
        email = pem;
        sexo = ps;
        cod = pcod;
    }

Instance in form

 private void btnAtualizar_Click(object sender, EventArgs e)
    {
        if (txtCodC.Text == "")
        {
            MessageBox.Show("Digite o código do cliente");
            txtCodC.Focus();
        }
        else
        {
            try
            {
                cliente cli = new cliente(txtNomeCliente.Text, txtEnderecoCliente.Text, Convert.ToInt32(txtTelefoneCliente.Text), txtEmailCliente.Text, cmbSexoCliente.Text, Convert.ToInt32(txtCodC.Text));
                cli.AlterarCliente();
                MessageBox.Show("Alterado com sucesso!");

            }
            catch (Exception ex) // Caso de erro, irá mostrar a mensagem de erro!
            {
                MessageBox.Show(ex.ToString()); // mensagem de erro
            }

        }
    }
    
asked by anonymous 12.11.2015 / 16:07

2 answers

2

There are several ways to do this. It depends a lot on your preference / need and some other factors like DataSource DataGridView .

First of all, your% of client% must be able to receive a parameter of type Form in its constructor. So, you only have to do the job of instantiating a Cliente object, sending it to Cliente and within Form you define which fields you are going to display, etc. Let's assume that Form is called Form . The constructor would look like this:

public FormEditarCliente(Cliente c){ ... }

I'll show you two ways you can do this

The first (and the one I use most) only works when its FormEditarCliente is populated by a DataGridView . Assuming List<T> is the data source of your grid.

The event List<Cliente> _clientes would look like this:

public static void dataGrid_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
     int idCliente = Convert.ToInt32(dataGrid["nomeDaColunaId", e.RowIndex].Value);
     var cliente = _clientes.Single(x => x.Id == idCliente);

     var form = new FormEditarCliente(cliente);
     form.ShowDialog();
}

What is done there is very simple. The first line takes the client id that is in CellDoubleClick (I assume the records have an Id and that Id is a primary key ), then a query is made in the list that serves as the source of data of DataGridView . This query consists of fetching a record in which the Id is equal to the Id that was captured from DataGridView . Then it is called DataGridView passing by parameter the client that was selected with Form .

The second (and the one I would use in your case after viewing the edit) is to just grab the client ID that is in Single() and then fetch this client in the database. data. Then pass it by parameter to DataGridView of editing, just as it was done before.

public static void dataGrid_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
     int idCliente = Convert.ToInt32(dataGrid["nomeDaColunaId", e.RowIndex].Value);
     Cliente cliente = BuscarClientePorId(idCliente);

     var form = new FormEditarCliente(cliente);
     form.ShowDialog();
}
    
12.11.2015 / 16:57
0

I consider the dataGrid and edit buttons and methods on the same form. So when an Insert, Update, or Delete occurs, you call the Select method to load the dataGrid again.

    
20.01.2016 / 19:54