Modify a cell and save its modification from a DataGridView

0

I am developing an application for the control of labels for the company where I work. I am working with a DataGridView which stores the data entered from the first Textbox as shown in the following image:

When the application starts, the DataGridView remains blocked until the "edit" button is pressed. What I intend to do is modify a cell which is of the DataGridView and when pressing the "Save" button it is updated and likewise the changes in the textboxes of both "Total" and "Divided by 5" in the same way are updated making their respective function.

Pressing the "insert" button has the function of inserting the records within the DataGridView, calculating the sum of each column with the contained records and also the total dividing it by 5.

Try to put in the "Save" button the same functions that I have to do the total and the division between 5, but I think that changing an element from the DataGridView does not respect the change that was made in the cell.

Annex the code that I have so far:

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

namespace Etiquetado
{
    public partial class BotonNino : UserControl
    {
        public BotonNino()
        {
            InitializeComponent();
            pictureBox2.Visible = false;

        }

        private void BotonNino_Load(object sender, EventArgs e)
        {
            ToolTip toolTip = new ToolTip();
            toolTip.SetToolTip(this.pictureBox1, "Editar");
            toolTip.SetToolTip(this.pictureBox2, "Guardar");

            DataGridViewTextBoxColumn Columna1 = new DataGridViewTextBoxColumn();
            Columna1.HeaderText = "17";
            Columna1.Width = 50;

            DataGridViewTextBoxColumn Columna2 = new DataGridViewTextBoxColumn();
            Columna2.HeaderText = "17.5";
            Columna2.Width = 50;

            DataGridViewTextBoxColumn Columna3 = new DataGridViewTextBoxColumn();
            Columna3.HeaderText = "18";
            Columna3.Width = 50;

            DataGridViewTextBoxColumn Columna4 = new DataGridViewTextBoxColumn();
            Columna4.HeaderText = "18.5";
            Columna4.Width = 50;

            DataGridViewTextBoxColumn Columna5 = new DataGridViewTextBoxColumn();
            Columna5.HeaderText = "19";
            Columna5.Width = 50;

            DataGridViewTextBoxColumn Columna6 = new DataGridViewTextBoxColumn();
            Columna6.HeaderText = "19.5";
            Columna6.Width = 50;

            DataGridViewTextBoxColumn Columna7 = new DataGridViewTextBoxColumn();
            Columna7.HeaderText = "20";
            Columna7.Width = 50;

            DataGridViewTextBoxColumn Columna8 = new DataGridViewTextBoxColumn();
            Columna8.HeaderText = "20.5";
            Columna8.Width = 50;

            DataGridViewTextBoxColumn Columna9 = new DataGridViewTextBoxColumn();
            Columna9.HeaderText = "21";
            Columna9.Width = 50;

            dataGridView1.Columns.Add(Columna1);
            dataGridView1.Columns.Add(Columna2);
            dataGridView1.Columns.Add(Columna3);
            dataGridView1.Columns.Add(Columna4);
            dataGridView1.Columns.Add(Columna5);
            dataGridView1.Columns.Add(Columna6);
            dataGridView1.Columns.Add(Columna7);
            dataGridView1.Columns.Add(Columna8);
            dataGridView1.Columns.Add(Columna9);

            dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dataGridView1.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            //DataTable dt = new DataTable();
            //dt.Columns.Add("Nombre");

            //dt.Columns.Add("Apellido");

            //this.dataGridView1.DataSource = dt;


        }


            private void button1_Click(object sender, EventArgs e)
            {


            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || textBox5.Text == "" || textBox6.Text == "" || textBox7.Text == "" || textBox8.Text == "" || textBox9.Text == "")
            {
                MessageBox.Show("Debes de ingresar todos los campos.");
            }
            else
            {

                this.dataGridView1.Rows.Add();
                int filas = this.dataGridView1.Rows.Count - 2;
                this.dataGridView1[0, filas].Value = textBox1.Text;
                this.dataGridView1[1, filas].Value = textBox2.Text;
                this.dataGridView1[2, filas].Value = textBox3.Text;
                this.dataGridView1[3, filas].Value = textBox4.Text;
                this.dataGridView1[4, filas].Value = textBox5.Text;
                this.dataGridView1[5, filas].Value = textBox6.Text;
                this.dataGridView1[6, filas].Value = textBox7.Text;
                this.dataGridView1[7, filas].Value = textBox8.Text;
                this.dataGridView1[8, filas].Value = textBox9.Text;




            }

            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
            textBox5.Clear();
            textBox6.Clear();
            textBox7.Clear();
            textBox8.Clear();
            textBox9.Clear();
        }


        private void pictureBox1_Click(object sender, EventArgs e)
        {

            MessageBox.Show("Ahora puedes editar la tabla");
            dataGridView1.Enabled = true;
            pictureBox2.Visible = true;
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {


    }

        private void dataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e)
        {
            if (botonGuardar == true) { 
                for (int numColumna = 0; numColumna < dataGridView1.Columns.Count; numColumna++)
                {
                    var sumTotal = 0;
                    var sumTotal1 = 0;
                    var sumTotal2 = 0;
                    var sumTotal3 = 0;
                    var sumTotal4 = 0;
                    var sumTotal5 = 0;
                    var sumTotal6 = 0;
                    var sumTotal7 = 0;
                    var sumTotal8 = 0;

                    foreach (DataGridViewRow row in dataGridView1.Rows)
                    {

                        sumTotal += Convert.ToInt32(row.Cells[0].Value);
                        sumTotal1 += Convert.ToInt32(row.Cells[1].Value);
                        sumTotal2 += Convert.ToInt32(row.Cells[2].Value);
                        sumTotal3 += Convert.ToInt32(row.Cells[3].Value);
                        sumTotal4 += Convert.ToInt32(row.Cells[4].Value);
                        sumTotal5 += Convert.ToInt32(row.Cells[5].Value);
                        sumTotal6 += Convert.ToInt32(row.Cells[6].Value);
                        sumTotal7 += Convert.ToInt32(row.Cells[7].Value);
                        sumTotal8 += Convert.ToInt32(row.Cells[8].Value);

                    }

                    textBox18.Text = sumTotal.ToString();//Aqui en cada iteración del for deberia ser el textbox correspondiente
                    textBox17.Text = sumTotal1.ToString();
                    textBox16.Text = sumTotal2.ToString();
                    textBox15.Text = sumTotal3.ToString();
                    textBox14.Text = sumTotal4.ToString();
                    textBox13.Text = sumTotal5.ToString();
                    textBox12.Text = sumTotal6.ToString();
                    textBox11.Text = sumTotal7.ToString();
                    textBox10.Text = sumTotal8.ToString();


                }

                textBox19.Text = (Convert.ToDouble(textBox18.Text) / Convert.ToDouble(5)).ToString();
                textBox20.Text = (Convert.ToInt32(textBox17.Text) / Convert.ToInt32(5)).ToString();
                textBox21.Text = (Convert.ToInt32(textBox16.Text) / Convert.ToInt32(5)).ToString();
                textBox22.Text = (Convert.ToInt32(textBox15.Text) / Convert.ToInt32(5)).ToString();
                textBox23.Text = (Convert.ToInt32(textBox14.Text) / Convert.ToInt32(5)).ToString();
                textBox24.Text = (Convert.ToInt32(textBox13.Text) / Convert.ToInt32(5)).ToString();
                textBox25.Text = (Convert.ToInt32(textBox12.Text) / Convert.ToInt32(5)).ToString();
                textBox26.Text = (Convert.ToInt32(textBox11.Text) / Convert.ToInt32(5)).ToString();
                textBox27.Text = (Convert.ToInt32(textBox10.Text) / Convert.ToInt32(5)).ToString();

            }
          }
        }
    }

I hope you explained, greetings.

Thank you.

    
asked by Ezequie Lopez 10.08.2018 в 01:26
source

3 answers

0

I finally managed to do it this way:

1. Create a CellClick event which when clicking on any of the cells to be modified shows that entire row within the textbox.

 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {

            textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
            textBox2.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
            textBox3.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
            textBox4.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
            textBox5.Text = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
            textBox6.Text = dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString();
            textBox7.Text = dataGridView1.Rows[e.RowIndex].Cells[6].Value.ToString();
            textBox8.Text = dataGridView1.Rows[e.RowIndex].Cells[7].Value.ToString();
            textBox9.Text = dataGridView1.Rows[e.RowIndex].Cells[8].Value.ToString();


        }

  • By pressing the "save" button, the DataGriedView is updated and performs the calculation functions "Total" and "Divided by 5".
  • private void pictureBox2_Click (object sender, EventArgs e)         {

            int poc;
            poc = dataGridView1.CurrentRow.Index;
    
            dataGridView1[0,poc].Value = textBox1.Text;
            dataGridView1[1, poc].Value = textBox2.Text;
            dataGridView1[2, poc].Value = textBox3.Text;
            dataGridView1[3, poc].Value = textBox4.Text;
            dataGridView1[4, poc].Value = textBox5.Text;
            dataGridView1[5, poc].Value = textBox6.Text;
            dataGridView1[6, poc].Value = textBox7.Text;
            dataGridView1[7, poc].Value = textBox8.Text;
            dataGridView1[8, poc].Value = textBox9.Text;
    
            for (int numColumna = 0; numColumna < dataGridView1.Columns.Count; numColumna++)
            {
                var sumTotal = 0;
                var sumTotal1 = 0;
                var sumTotal2 = 0;
                var sumTotal3 = 0;
                var sumTotal4 = 0;
                var sumTotal5 = 0;
                var sumTotal6 = 0;
                var sumTotal7 = 0;
                var sumTotal8 = 0;
    
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
    
                    sumTotal += Convert.ToInt32(row.Cells[0].Value);
                    sumTotal1 += Convert.ToInt32(row.Cells[1].Value);
                    sumTotal2 += Convert.ToInt32(row.Cells[2].Value);
                    sumTotal3 += Convert.ToInt32(row.Cells[3].Value);
                    sumTotal4 += Convert.ToInt32(row.Cells[4].Value);
                    sumTotal5 += Convert.ToInt32(row.Cells[5].Value);
                    sumTotal6 += Convert.ToInt32(row.Cells[6].Value);
                    sumTotal7 += Convert.ToInt32(row.Cells[7].Value);
                    sumTotal8 += Convert.ToInt32(row.Cells[8].Value);
    
                }
    
                textBox18.Text = sumTotal.ToString();//Aqui en cada iteración del for deberia ser el textbox correspondiente
                textBox17.Text = sumTotal1.ToString();
                textBox16.Text = sumTotal2.ToString();
                textBox15.Text = sumTotal3.ToString();
                textBox14.Text = sumTotal4.ToString();
                textBox13.Text = sumTotal5.ToString();
                textBox12.Text = sumTotal6.ToString();
                textBox11.Text = sumTotal7.ToString();
                textBox10.Text = sumTotal8.ToString();
    
    
            }
    
            textBox19.Text = (Convert.ToDouble(textBox18.Text) / Convert.ToDouble(5)).ToString();
            textBox20.Text = (Convert.ToDouble(textBox17.Text) / Convert.ToDouble(5)).ToString();
            textBox21.Text = (Convert.ToDouble(textBox16.Text) / Convert.ToDouble(5)).ToString();
            textBox22.Text = (Convert.ToDouble(textBox15.Text) / Convert.ToDouble(5)).ToString();
            textBox23.Text = (Convert.ToDouble(textBox14.Text) / Convert.ToDouble(5)).ToString();
            textBox24.Text = (Convert.ToDouble(textBox13.Text) / Convert.ToDouble(5)).ToString();
            textBox25.Text = (Convert.ToDouble(textBox12.Text) / Convert.ToDouble(5)).ToString();
            textBox26.Text = (Convert.ToDouble(textBox11.Text) / Convert.ToDouble(5)).ToString();
            textBox27.Text = (Convert.ToDouble(textBox10.Text) / Convert.ToDouble(5)).ToString();
    
    
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
            textBox5.Clear();
            textBox6.Clear();
            textBox7.Clear();
            textBox8.Clear();
            textBox9.Clear();
    
            MessageBox.Show("Cambios registrados exitosamente");
    
            pictureBox2.Visible = false;
            pictureBox3.Visible = false;
            button1.Visible = true;
            dataGridView1.Enabled = false;
            label13.Visible = false;
            label14.Visible = false;
        }
    

    Greetings, thank you.

        
    answered by 10.08.2018 / 19:58
    source
    2

    One more solution to yours is:

  • Perform your calculations in a separate method so that you can reuse in any event of the DataGridView or in some other button.
  • In this case I call it sumaColumas .

    public void sumaColumnas()
    {
        var sumTotal1 = 0;
        var sumTotal2 = 0;
        var sumTotal3 = 0;
        //... mas variables
    
        foreach (DataGridViewRow row in DataGridView1.Rows)
        {
            sumTotal1 += Convert.ToInt32(row.Cells[0].Value);
            sumTotal2 += Convert.ToInt32(row.Cells[1].Value);
            sumTotal3 += Convert.ToInt32(row.Cells[2].Value);
            //... más calculos
        }
    
        txtTotal1.Text = sumTotal1.ToString();
        txtTotal2.Text = sumTotal2.ToString();
        txtTotal3.Text = sumTotal3.ToString();
        //... más cajas de texto
    
        // ----
        txtDiv1.Text = Convert.ToDouble(txtTotal1.Text) / 5;
        txtDiv2.Text = Convert.ToDouble(txtTotal2.Text) / 5;
        txtDiv3.Text = Convert.ToDouble(txtTotal3.Text) / 5;
        //... Mas divisiones
    }
    

    and when you insert new rows with your insert button, just send the method and update your text fields with the new calculations.

    example.

    private void btnInsertar_Click(System.Object sender, System.EventArgs e)
    {
        // … tu código de configuración de columnas, etc
    
        // agregas una nueva columna
        DataGridView1.Rows.Add(txtVal1.Text, txtVal2.Text, txtVal3.Text);
    
        // Llamas el método sumar(al ultimo para que tome los nuevos valores agregados)
        sumaColumnas();
    }
    

    In DatagridView has a behavior like those of Excel, when modifying a cell what you do is double click on a cell and the cursor appears to modify the value, you replace the value and with a valid enter the modification, the same is can perform in this case and to detect the change in a dataGridView cell there is an event of the same called CellEndEdit and within that calls to call the created method and it will do the calculations taking the new value.

    private void DataGridView1_CellEndEdit(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
    {
        sumaColumnas();
    }
    

    As I said from the beginning, it is one more idea to your solution, that is what this world is about, several solutions to a problem ... Greetings.

        
    answered by 10.08.2018 в 21:34
    1

    The best thing you can do is instead of calling the function that calculates the values of the TextBox in the events of the buttons, your own would be to perform the calculation and modify the TextBox in the event RowValidated it would look like this:

    In the designer you add the datagrid event in this way:

     this.dataGridView1.RowValidated += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgv_RowValidating);
    

    And in this event you perform the calculations that you were previously doing in the events of the buttons:

    private void dgv_RowValidating(object sender, DataGridViewCellEventArgs e)
    {
        //Aquí iría el código que calcula los valores por columnas y los añade a los textbox
    
    }
    

    I hope it serves you. Greetings

        
    answered by 10.08.2018 в 09:40