Datagridview - Edit by Currentrow.Index does not leave Index 1

Asked

Viewed 276 times

1

Note: I’m learning to use Datagrid, so I’m testing some examples.

I have this form:

inserir a descrição da imagem aqui

And this code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProjetoFinal
{
    public partial class frmCadastroClientes : Form
    {
        int i = 0;
        int poc;

        public frmCadastroClientes()
        {
            InitializeComponent();
        }

        private void btnGravar_Click(object sender, EventArgs e)
        {
            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!");
        }
    }
}

When I click on a line its values fill in the form fields, so I can edit them, but whenever I edit another line other than the first one it does not change and only changes the first line.

Example: I have Id: 2, I changed the city of: Salto for Itu but what changed was the Id: 1 Campinas for Itu.

I hope I’ve been clear in what I’m asking for help.

  • Instead of using dgvClientes.CurrentRow.Index try to use e.RowIndex which takes the index of the clicked line.

  • You do not have this property to use :d

  • I’m using this video as a base: https://goo.gl/MQ7Sd3 at 22:00min it makes it look identical.. but I don’t know what I need to edit the line I clicked.

1 answer

0


Well, I found where the mistake was.

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 is int poc should only be poc because I had already declared it to be 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;

        }

Browser other questions tagged

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