Hi every one. I am having some problem with calculation in datagridview. i have 3 columns in datagridview which are Date,location,Payment. What i want to do now is i want to calculate the total of the payment and my total is a textfield out side the data gridview under the payment field with the label total. This payment is for a specific person who books a photo shoot.suppose when customer 1st comes in the store he pays 30 pounds and second time he pays 20 pounds so total should come up as 50.every time customer comes total for that particular customer should change.please help.Or if possible can some one provide the code

You will need a seperate table for the customer.

Something Like


::Table Name: Cust_info::
::Column 1:cust_id::
::Column 2:cust_name::
::Column 3:cust_charges::
::Column 4:cust_payed::

Edited 4 Years Ago by Begginnerdev: n/a

i have a booking table where cust_id is used as foreign key . so All together there are 5 columns. bk_id,cust_id,date,location,payment.
but i dont know how to calculate the total and display the result in a textbox.Each time new payment is made.The total should change accordingly,

You could:

'Fist grab the total

try
Dim s As String = "SELECT payment FROM customer WHERE cust_id=" & cusID '< A variable that you asign the value to.
Dim cmd As new OLEDBCommand(sqls,con)'Where con is OLEDBConnection(See URL Below)
'Be sure to open the connection
con.Open()
Dim da As OLEDBDataAdapter
Dim ds As New DataSet
Dim dt As New DataTable
da.SelectCommand = cmd
da.Fill(ds,"GetInfo")
dt = ds.Tables("GetInfo")

'Then you will need to pass the value into a variable.
Dim amount As Double = dt.Rows(0).Item(0)

amounr += paymentAmnt 'Amount the customer payed.

'Now you need to update the table.

s = "UPDATE customer SET amount=" & amount & " WHERE cust_id=" & cusID
cmd = New OleDBCommand(sqls,con)
cmd.ExecuteNonQuery
Catch ex As Exception
   MsgBox("Exception From:" & ex.Source & vbcrlf & ex.message)
End Try

Here is the URL

Edited 4 Years Ago by Begginnerdev: n/a

i have written this code which is doing the job but the only problem i m having is, when i add a new payment than i have to enter a key in the total textfield than it shows me the total. what i want to do is, straight away when i add a payment it should add all the total and should show in textfield.

If Me.BookingBindingSource.Position > -1 Then
Dim payment As Double = 0
For Each row As DataRowView In Me.BookingBindingSource.List
payment += row!payment

Next
Me.TextBox1.Text = Format(payment, "c")
Else
Me.TextBox1.Text = ""
End If

Edited 4 Years Ago by dilse4sk: n/a

Fire the code off from the payment textbox's Leave event.

That way after you leave the payment text box, the total is calculated.

Edited 4 Years Ago by Begginnerdev: n/a

hi problem is still not solved. As i am using data gridview to insert the payment i still have to either click or press some key to display the result in the text box. i have cut and pasted the code in textbox leave event,

Hi every one. I am having some problem with calculation in datagridview. i have 3 columns in datagridview which are Date,location,Payment. What i want to do now is i want to calculate the total of the payment and my total is a textfield out side the data gridview under the payment field with the label total. This payment is for a specific person who books a photo shoot.suppose when customer 1st comes in the store he pays 30 pounds and second time he pays 20 pounds so total should come up as 50.every time customer comes total for that particular customer should change.please help.Or if possible can some one provide the code

You have 3 columns in datagrid view. You want to calculate payment in column 3 and show it on textbox.

Private Sub DataGridView1_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
    Dim temp As Integer
    For i As Integer = 0 To DataGridView1.Rows.Count - 2
        temp += CInt(DataGridView1.Item(2, i).Value.ToString)
    Next i
    TextBox1.Text = temp
End Sub

Edited 4 Years Ago by Jx_Man: n/a

to calculate the total payment use this code .

'for example you have a grid having column name payment .
'now use this code at the button click event

dim i as int 
dim total as decimal
for i=0 to datagrid.rows.count -1 
total = total + val(datagrid.item("payment",i).value.tostring)' or you can use Cint
next
textboxTotal.text= total

this code will calculate all the payments and show total in the textbox

Hope this will solved your prob.

Regards

waqas bro i used this code under button click event but i get an error column named payment cannot be found.i have a column named Payment is datagridview i am sending u screen shot aswell so that you can have a look.

Private Sub btnTotal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTotal.Click
Dim i As Integer
Dim total As Decimal
For i = 0 To NewBookingDataGridView.Rows.Count - 1
total = total + Val(NewBookingDataGridView.Item("Payment", i).Value.ToString) ' or you can use Cint
Next
TextBox1.Text = total
End Sub

Attachments booking.jpg 35.53 KB

Have you tried using the Cell.Leave event?


Like this:

Private Sub DataGridView1_CellLeave(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellLeave
    'Your Code here
End Sub

OR you can try:

Private Sub DataGridView1_CellValueChanged(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
   'Your Code here
End Sub

But I have never used ValueChanged and you may run into problems with an infinitie loop.

Cell Value Change > Calculate Total > Post Total > Cell Value Change.....

Edited 4 Years Ago by Begginnerdev: n/a

ok then used this ,hope this will solve your issue.

dim i as int 
dim total as decimal
for i=0 to datagrid.rows.count -1 
total = total + val(datagrid.item(4,i).value.tostring)' or you can use Cint
next
textboxTotal.text= total

try this .

waqas bro where shall i use this code.i mean under a button or gridview or which event.plz explain

can you please help me with my problem about storing values from the textbox in the datagridview after clicking the calculate button. Can you give me some codes for this.

This article has been dead for over six months. Start a new discussion instead.