Home > front end >  DataGridView show null decimal value
DataGridView show null decimal value

Time:06-06

I'm programming an app in C# and I would like to show the data from the textboxes in the dgv. However, when I enter the value through the cart button, the value appears at 0 (as shown in the image below). can anybody help me?

Running program

Here's the code:

dgv_Carrinho.ColumnCount = 4;
            dgv_Carrinho.Columns[0].Name = "Produto";
            dgv_Carrinho.Columns[1].Name = "Valor";
            dgv_Carrinho.Columns[2].Name = "Quantidade";
            dgv_Carrinho.Columns[3].Name = "Hora";
            dgv_Carrinho.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dgv_Carrinho.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dgv_Carrinho.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            // Casa decimal DataGridView
            dgv_Carrinho.Columns[1].DefaultCellStyle.Format = "N2";
            // Introduçãol das textoxes na datagridview
            dgv_Carrinho.Rows.Add(txt_Produto.Text, subtotal, cmb_Quantidade.Text, txt_Hora.Text);

Ps.: Carrinho = cart (like supermarket cart); Gravar = save; Produtos = Products; Hora = Hour; Quantidade = Quantity;

CodePudding user response:

dgv_Carrinho.Rows.Add(txt_Produto.Text, subtotal, cmb_Quantidade.Text, txt_Hora.Text);

Based on this line of code, we could see that the problem is with subtotal.

There is no definition and operation of subtotal in your code.

Try adding similar code:

(Assume that the corresponding textbox is named txt_Valor)

dgv_Carrinho.Rows.Add(txt_Produto.Text, txt_Valor.Text, cmb_Quantidade.Text,txt_Hora.Text);

CodePudding user response:

You add a row to you dgv_Carrinhh with the value of Subtotal. But maybe nothing will update this in the DGV. You have to change the value each time you Subtotal change, with something like this

dgv_CArrinho.Rows[0].Cells[1].Value = subtotal

But this is not a bit heavy to use. you can check the concept of datasource, to link a classe to you datagridview and dont have to manually set the value each time.

https://docs.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-bind-data-to-the-windows-forms-datagridview-control?view=netframeworkdesktop-4.8

  • Related