4
Good morning to all.
Next staff, I have a (query) form that contains the Datagrideview. And the registration form in another form.
I would like to understand, how can I double click on a line of Datagrideview he send all the data from the line to this registration form (so I can crud to change).
Image to illustrate:
Datagrid class
class datagride
{
banco conexao = new banco();
// Atributos
private string nomeTabela;
private DataGridView nomeDataGride;
// Construtor
public datagride(string pnt, DataGridView pndg)
{
nomeTabela = pnt;
nomeDataGride = pndg;
}
public datagride(DataGridView pndg)
{
nomeDataGride = pndg;
}
// Método
public DataGridView carregarGride(string sql = "")
{
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 the 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
}
}
}
Man, this print is awful :p
– Jéf Bueno
I tried to improve @jbueno :)
– WSS
It’s much better. I’m writing an answer. It would help me a lot if you put the code you use to popular Datagrid
– Jéf Bueno
Okay, I’ve updated @jbueno
– WSS
Take a look at my answer. I tried to be as generic as possible, because without knowing your model is very complicated.
– Jéf Bueno