-
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
-
Try using a sql statement like this:
Code:
select column1, column2, (convert(float,column1) + convert(float,column2)) as column3 from [tblYourTable]
-
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
-
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.
-
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!