Results 1 to 5 of 5

Thread: Calculation in DataGrid

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2000
    Posts
    140
    I am using a DataGrid which bounds to a table (access). The DataGrid has three columns, they all in currency formats. What I would like to see is column3 is the sum of column1 and column2. In other words, after the amount have been enter to column1 and column2, column3 will calculate the sum and show it.

    Can this be done in SQL statement or in code ??? Pls provide sample code.


    Thank you in advancew

  2. #2
    Junior Member
    Join Date
    Oct 2000
    Location
    Purgatory
    Posts
    31
    Try using a sql statement like this:

    Code:
    select column1, column2, (convert(float,column1) + convert(float,column2)) as column3 from [tblYourTable]

  3. #3
    Guest
    If you mean that you're gonna have to enter in data in Columns 1 and 2, try the datagrid's AfterColUpdate event. Here's a sample:

    Code:
    dgSample_AfterColUpdate(ByVal ColIndex as Integer)
    Dim number1, number2, sum as Double 
    
      If (ColIndex = 0 And (Len(Trim(dgSample.Columns(1).text)) > 0) Or _
        If (ColIndex = 1 And (Len(Trim(dgSample.Columns(0).text > 0) Then
    
          number1 = CDbl(dgSample.Columns(0).text)
          number2 = CDbl(dgSample.Columns(1).text)
    
          sum = number1 + number2
          dgSample.Columns(2).text = sum
     
    End Sub

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Feb 2000
    Posts
    140
    Hi billyboy,

    Can you pls look at your code again... Something is missing I couldn't figure out what.

    If (ColIndex = 0 And (Len(Trim(dgSample.Columns(1).text)) > 0) Or _
    If (ColIndex = 1 And (Len(Trim(dgSample.Columns(0).text > 0) Then

    Is this two If statements or one??? I even add an end if statement and it still give me an error message.

    Thanks.

  5. #5
    Guest

    Talking forgot something!

    Code:
    dgSample_AfterColUpdate(ByVal ColIndex as Integer)
    Dim number1, number2, sum as Double 
    
      If (ColIndex = 0 And (Len(Trim(dgSample.Columns(1).text)) > 0) Or _
        If (ColIndex = 1 And (Len(Trim(dgSample.Columns(0).text)) > 0) Then
    
          number1 = CDbl(dgSample.Columns(0).text)
          number2 = CDbl(dgSample.Columns(1).text)
    
          sum = number1 + number2
          dgSample.Columns(2).text = sum
     
    End Sub
    I forgot to put in the two closing parentheses on the second conditional line..sorry! if you try to copy and paste this it should work.

    sorry again!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width