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.