VB.Net How To Calculate Two Column In DataGridView

Developer and programmer find the the best way to customize their program according to customer requirement. Everyday the learn and explore different code on the internet with different search engine.

So I am writing this tutorial those who want to make DatagridView with custom calculation like ms excel with user friendly. Or those who do want to use text box calculation, especially when he developing POS programming for make order  and invoices with different ways.

Before start this tutorial you must read my previous article how to search and delete specific record in DataGridView VB.Net.

So in this  tutorial how to calculate tow column in DataGaridView VB.Net we will cover following topics.

  1. Numeric Data Type
  2. Read DataGridView Row and Column Index.
  3. Put The data in Cells.
  4. Use textboxes for show grand total summary of bill.
How To Calculate Two Column In DataGridView

How To Calculate Two Column In DataGridView

Step 01.     To Calculate Two Column In DataGridView you need to insert or drag DataGridView control from the control box on the Form.

Step 02.    In the property window on the write side choose column collection and add the desired column.

Step 03: Add the two text boxes and two label place these on bottom of DataGridVeiw . One for calculate total quantity and 2nd for Total Bill Amount.

1. Declare The Numeric Data Type

To add the variable double  click on the DataGaridView And choose cellEndEdit event and past the following code on it.

Private Sub dgw_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgw.CellEndEdit

Dim amount As Decimal = 0
Dim gtotal As Decimal = 0
Dim qty As Double = 0
End Sub

2. Use For Loop And Read DataGridView Rows And Column Index

In 2nd step we get the cell index and multiply qty with product price and get value in third column. So copy this code and past it below the numeric data type.

For Each r As DataGridViewRow In Me.dgw.Rows
'multiply qty with unit price
amount = r.Cells(2).Value * r.Cells(3).Value
r.Cells(4).Value = amount


3. Get The Summary Of Order

Once again we will use the For loop to get the order summary. In summary we need Total order qty and total order amount.


For index As Integer = 0 To dgw.RowCount - 1
gtotal += Convert.ToDecimal(dgw.Rows(index).Cells(4).Value)
qty += Convert.ToDecimal(dgw.Rows(index).Cells(3).Value)

txtqty.Text = qty
txtamount.Text = gtotal

Leave a Reply