DataGridView - Edit by CurrentRow.Index does not exit Index 1

1
  

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.

    
asked by anonymous 25.10.2017 / 14:22

1 answer

0

Well, I found the error.

Wrong:

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;
        }

Where int poc should be only poc since it has already declared that it is variable up there.

Correct:

private void dgvClientes_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            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;

        }
    
25.10.2017 / 15:17