7 Replies - 735 Views - Last Post: 18 June 2012 - 11:40 AM Rate Topic: -----

#1 m16piggy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-June 12

How do I add entries from a database to add up to a total?

Posted 17 June 2012 - 09:53 PM

I've made a program for a small business that keeps track of what items there are, their prices, and the quantities of those items. I would like to add a button that goes along the lines of "Add to sale" or "Add to cart", and have it select the currently highlighted item in the database grid view to add the price column up, to make a total for a sale.

Example;
User selects "Sun jar" item from the database grid view (this item has a price of $29.95)
User clicks "Add to cart" based on how many the customer is buying, and it will add up $29.95 each time the button is pressed, putting the total in a label
If the user accidentally puts in too many of the same item, they can click "Remove from cart" and it will subtract the price of the current total.

If I'm posting this incorrectly, or if there's anything I need to add, please tell me, I'm new here.

Is This A Good Question/Topic? 0
  • +

Replies To: How do I add entries from a database to add up to a total?

#2 rgfirefly24  Icon User is online

  • D.I.C Lover
  • member icon


Reputation: 267
  • View blog
  • Posts: 1,476
  • Joined: 07-April 08

Re: How do I add entries from a database to add up to a total?

Posted 18 June 2012 - 05:46 AM

You would want a variable to store the running total. Basically in the click event of Add to Cart you would increment the variable by the price of the item

variable += price



You would also in the Remove from cart button decrement the variable the price of the item being removed

variable -= price 



then you can set the Text property of the label to be equal to that variable at the end of each of the click events.

textbox.Text = variable


Was This Post Helpful? 1
  • +
  • -

#3 m16piggy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-June 12

Re: How do I add entries from a database to add up to a total?

Posted 18 June 2012 - 10:38 AM

Thanks a lot for that, that was simple and I'm surprised didn't get that.

I've got the variable part of it down, but how would I access the price column of the datagrid view, or the database all together? I'm really new at databases, as you can probably tell.
Was This Post Helpful? 0
  • +
  • -

#4 rgfirefly24  Icon User is online

  • D.I.C Lover
  • member icon


Reputation: 267
  • View blog
  • Posts: 1,476
  • Joined: 07-April 08

Re: How do I add entries from a database to add up to a total?

Posted 18 June 2012 - 10:49 AM

My suggestion is to create an object called Item which consists of Description, and price (and anything else relavant to the item). Once you do that you can fill the list from the database and then set the datasource of the datagrid to that list. This will also allow you to easily get any information you need just by having the unique identifier for the item. You would also use linq to get the information from the list when a user clicks add to cart. Also is this a web application or desktop application?
Was This Post Helpful? 0
  • +
  • -

#5 m16piggy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-June 12

Re: How do I add entries from a database to add up to a total?

Posted 18 June 2012 - 10:54 AM

Since there is only one main computer here, there isn't a need for an SQL server database, so it's just a local desktop application, if that helps.

So, I'm a bit confused. Is there no way to identify a column and it's value?
Was This Post Helpful? 0
  • +
  • -

#6 rgfirefly24  Icon User is online

  • D.I.C Lover
  • member icon


Reputation: 267
  • View blog
  • Posts: 1,476
  • Joined: 07-April 08

Re: How do I add entries from a database to add up to a total?

Posted 18 June 2012 - 11:26 AM

There is, but it's easier to use objects then to try and work with the datagrid directly.

Where are your items stored? or how do you have your grid bound right now?

If you use a list of objects you can do this:

'This is in an Item.vb file
Public Class Item
	Public Property ItemID() As String
	Public Property Description() As String
	Public Property Price() As Double
End Class



Then within your form1.vb file you can do this:
'NOTE: I'm creating random data, but you would fill in the List based off what ever your data source is
Public Class Form1
	Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
		Dim items As List(Of Item) = New List(Of Item) ' Here we create the List(Of T) to store the Item Objects
		Dim Item1 As Item = New Item
		Dim Item2 As Item = New Item

		Item1.Description = "Item 1"
		Item1.ItemID = 1
		Item1.Price = 29.95
		items.Add(Item1)
		Item2.Description = "Item 2"
		Item2.ItemID = 2
		Item2.Price = 49.99
		items.Add(Item2)

		DataGridView1.DataSource = items
	End Sub
End Class



Then in your button handler you could do something like so:

Dim selectedRows As DataGridViewSelectedRowCollection = DataGridView1.SelectedRows

		For Each _row As DataGridViewRow In selectedRows
			Dim itemPrice = (From i In items
			 Where i.ItemID = _row.Cells(0).Value
			 Select i.Price).FirstOrDefault()

			CartTotal += itemPrice
		Next

		TextBox1.Text = CartTotal



This will give you flexability and DataGridViews are ment for just that, viewing data. Everything you do with Item should be done with the list of objects.

This post has been edited by rgfirefly24: 18 June 2012 - 11:47 AM

Was This Post Helpful? 0
  • +
  • -

#7 m16piggy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-June 12

Re: How do I add entries from a database to add up to a total?

Posted 18 June 2012 - 11:34 AM

Well, that aside, I have tried a few methods. I have my program set to when a user selects something from the datagrid, they can edit it through textboxes next to it and click "Save." I'm now trying to add the value of the price textbox to an integer, to be displayed, as this seems easier to me. Creating the object on the other hand... not so familiar with it. Creating an object bound to some data? I've tried
c = +Val(PriceTextBox.Text)
, but to no avail.
Was This Post Helpful? 0
  • +
  • -

#8 m16piggy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-June 12

Re: How do I add entries from a database to add up to a total?

Posted 18 June 2012 - 11:40 AM

EDIT; I have got it all working, but the variable rounds up, instead of 29.95 it'll put $30. Any way to fix this?
Code;
c -= Val(PriceTextBox.Text)

c += (Val(PriceTextBox.Text))

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1