8 Replies - 4143 Views - Last Post: 04 June 2011 - 10:15 PM Rate Topic: -----

#1 methodman2004_30  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-July 08

Function to Add and Subtract Values on a specific Column

Posted 01 June 2011 - 05:33 PM

Hello Guys,

I would request for help, i have a problem regarding datagrid.remove and textbox. This is a Function to Add and Subtract Values on a specific Column.
If I cellclick on the main datagrid on a specfic row which has record where the record goes to the other datagrid with no record yet to be filled with record, the textbox will count/ add all the values from the UNITS COLUMN and in an instance on one row removed in the other datagrid, the total value of units in the textbox does not subtract or less.
here's the code. Please help me fix this problem guys.

  Public Function total_units_remove() As Double

        Dim totalunits As Double = 0
        Dim i As Integer = 0
        For i = 0 To dgvSubjectsQueued.Rows.Count - 1
            totalunits = totalunits - (dgvSubjectsQueued.Rows(i).Cells(2).Value)
            txttotalunits.Text = totalunits
        Next i
        Return total_units()

        End Function 




but on the other hand, the compute on the addition of units is working.
here is the code.

  Public Function total_units() As Double
        Dim totalunits As Double = 0
        Dim i As Integer = 0
        For i = 0 To dgvSubjectsQueued.Rows.Count - 1
            totalunits = totalunits + Convert.ToDouble(dgvSubjectsQueued.Rows(i).Cells(2).Value)
            txttotalunits.Text = totalunits
        Next i
        Return total_units
        End Function



Is This A Good Question/Topic? 0
  • +

Replies To: Function to Add and Subtract Values on a specific Column

#2 methodman2004_30  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-July 08

Re: Function to Add and Subtract Values on a specific Column

Posted 01 June 2011 - 06:08 PM

HELLO GUYS?
Was This Post Helpful? 0
  • +
  • -

#3 methodman2004_30  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-July 08

Re: Function to Add and Subtract Values on a specific Column

Posted 02 June 2011 - 04:19 PM

HELP ME GUYS...
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Function to Add and Subtract Values on a specific Column

Posted 02 June 2011 - 05:14 PM

Ok, many differences:
totalunits = totalunits - (dgvSubjectsQueued.Rows(i).Cells(2).Value)
Make conversion as you did in add, looks cool
Also you set Dim totalunits As Double = 0, this means the first value will be 0-something. This does not give logic here is subtraction
Was This Post Helpful? 1
  • +
  • -

#5 methodman2004_30  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-July 08

Re: Function to Add and Subtract Values on a specific Column

Posted 02 June 2011 - 06:17 PM

So what shall I do?
Was This Post Helpful? 0
  • +
  • -

#6 methodman2004_30  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-July 08

Re: Function to Add and Subtract Values on a specific Column

Posted 03 June 2011 - 04:33 AM

Hello Guys? Help me Please
Was This Post Helpful? 0
  • +
  • -

#7 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1752
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Function to Add and Subtract Values on a specific Column

Posted 03 June 2011 - 12:37 PM

Subtraction is different from addition. Like to say a+b = b+a, but a-b!=b-a. So when we talk about subtraction many things comes across, which one is taken from which one, which one is the biggest in the list(may be) & what do you want to get here when subtracting.
Assume a column has the following data: 1,2,3,4,5. What do you expect here!
Was This Post Helpful? 0
  • +
  • -

#8 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 69
  • View blog
  • Posts: 310
  • Joined: 12-January 11

Re: Function to Add and Subtract Values on a specific Column

Posted 03 June 2011 - 01:03 PM

Well, I see what may be a problem, but I don't have the whole picture (see code below with comments:

Public Function total_units_remove() As Double 

'here you set your total units to zero, meaning any subtraction will result in a negative number.  
'As a side note, you can just leave the "=0" off as it defaults to "0" when you instanciate it.
Dim totalunits As Double
'This is unnecessary as you can instanciate and use it within the For loop 
'Dim i As Integer = 0  

For i = 0 To dgvSubjectsQueued.Rows.Count - 1
'so now you have a number that you set to zero and you are subtracting from that number right off
'the bat, so you will always get a number <=0
 totalunits = totalunits - (dgvSubjectsQueued.Rows(i).Cells(2).Value)  
 txttotalunits.Text = totalunits
'no i needed  
Next 
 
total_units_remove= total_units()  

End Function


Was This Post Helpful? 0
  • +
  • -

#9 methodman2004_30  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-July 08

Re: Function to Add and Subtract Values on a specific Column

Posted 04 June 2011 - 10:15 PM

Friend psyguy and the others,

well it quite worked, as i applied the cellclick event, rows were added on the other datagrid and it added the values of subject units in the column (dgvSubjectsQueued.Rows(i).Cells(2).Value), example there are 5 ROWS and in that (dgvSubjectsQueued.Rows(i).Cells(2).Value) COLUMN having 3 UNITS each, now i applied adding the SUBJECT UNITS to get the TOTAL UNITS, so it added it up, 5*3=15. Now for subtraction it subtracted but there is 3 unit that remianed on the textbox when i applied removing subjects on the grid applying cellclick.

What may be the problem? thanks guys..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1