Note: I am learning to use the DataGrid, so I am testing some examples.
I have this form:
Andthiscode:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceProjetoFinal{publicpartialclassfrmCadastroClientes:Form{inti=0;intpoc;publicfrmCadastroClientes(){InitializeComponent();}privatevoidbtnGravar_Click(objectsender,EventArgse){i++;dgvClientes.Rows.Add(i,txtNome.Text,txtEnd.Text,txtCidade.Text,txtEstado.Text,maskFone.Text,txtEmail.Text);txtNome.Text="";
txtEnd.Text = "";
txtCidade.Text = "";
txtEstado.Text = "";
maskFone.Text = "";
txtEmail.Text = "";
}
private void btnLimpar_Click(object sender, EventArgs e)
{
txtNome.Text = "";
txtEnd.Text = "";
txtCidade.Text = "";
txtEstado.Text = "";
maskFone.Text = "";
txtEmail.Text = "";
btnGravar.Enabled = true;
}
private void dgvClientes_CellClick(object sender, DataGridViewCellEventArgs e)
{
int poc = dgvClientes.CurrentRow.Index;
txtNome.Text = dgvClientes[1, poc].Value.ToString();
txtEnd.Text = dgvClientes[2, poc].Value.ToString();
txtCidade.Text = dgvClientes[3, poc].Value.ToString();
txtEstado.Text = dgvClientes[4, poc].Value.ToString();
maskFone.Text = dgvClientes[5, poc].Value.ToString();
txtEmail.Text = dgvClientes[6, poc].Value.ToString();
btnGravar.Enabled = false;
}
private void btnEditar_Click(object sender, EventArgs e)
{
dgvClientes[1, poc].Value = txtNome.Text;
dgvClientes[2, poc].Value = txtEnd.Text;
dgvClientes[3, poc].Value = txtCidade.Text;
dgvClientes[4, poc].Value = txtEstado.Text;
dgvClientes[5, poc].Value = maskFone.Text;
dgvClientes[6, poc].Value = txtEmail.Text;
MessageBox.Show("Cliente número: " + i + "Alterado!");
}
}
}
As long as I click on a line the values of it fill the fields of the form, so that I can edit them, however whenever I edit another line other than the 1st line it does not change and only changes the first line.
Example: I have ID: 2, I changed the city from: Salto
to Itu
but what changed was Id: 1 from Campinas
to Itu
/ p>
I hope I've been clear on what I'm asking for help.