Showing posts with label VB. Show all posts
Showing posts with label VB. Show all posts

Friday, November 06, 2009

DataGridView Calculate Sum Total of Column or Row VB

I had a POS application is was working on for LogoMagik Custom T-Shirts in Toronto and I needed to caluculate the Quantity of a Line Item and display it on the Total Price cell for that row of the DataGridView


I found this VB code here and was pulling my hair out until i realized one thing...


I had not changed the Design Name of the columns so they were still that default names like DataGridViewTextBoxColumn1 , DataGridViewTextBoxColumn2 , DataGridViewTextBoxColumn6 , etc


DOH!

So in case this happens to you, be sure to check that the Design Name in the Bound Column Properties in the Edit Columns dialog that pops up when you choose to edit columns of a DataGridView is something other than the default name.



Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
' If the column is the Quantity column, compute the line total.
If Me.DataGridView1.Columns(e.ColumnIndex).Name = "QuantityColumn" Or Me.DataGridView1.Columns(e.ColumnIndex).Name = "SalePriceColumn" Then
Dim dblSalePrice As Double
Dim dblQuantity As Double
Dim dblLineTotal As Double
dblSalePrice = CDbl(Me.DataGridView1.CurrentRow.Cells("SalePriceColumn").Value)
dblQuantity = CDbl(Me.DataGridView1.CurrentRow.Cells("QuantityColumn").Value)
dblLineTotal = dblSalePrice * dblQuantity
Me.DataGridView1.CurrentRow.Cells("LineTotalColumn").Value = dblLineTotal
End If
End Sub