Problem updating value in a DataGrid cell. [Resolved]
I have a DataGrid, which has 3 columns:
- Net Price
- Quantity
- Total Price
I want the DataGrid to act like this:
- Net Price is set.
- User enter Quantity.
- Total Price is calculated automatically (Net Price * Quantity).
- Total Price column will be updated automatically.
So I wrote the code like this:
Code:
'Calculate total price
Private Sub DataGrid_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid.CurrentCellChanged
Dim intPreviousRow As Integer
Dim intColumn As Integer = DataGrid.CurrentCell.ColumnNumber
If DataGrid.CurrentCell.RowNumber <> 0 Then
intPreviousRow = DataGrid.CurrentCell.RowNumber - 1
Else
intPreviousRow = DataGrid.CurrentCell.RowNumber
End If
Dim intTaken As Integer = DataGrid.Item(intPreviousRow, intColumn)
Dim dblNetPrice As Double = DataGrid.Item(intPreviousRow, intColumn + 1)
Dim dblTotalPrice As Double = intTaken * dblNetPrice
DataGrid.Item(intPreviousRow, intColumn + 2) = dblTotalPrice
End Sub
Everytime I enter the Quantity, this error message pops up:
"An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dll
Additional information: The ListManager's position must be equal to rowNum."
Why? I can't solve this. I think the code I wrote cannot work.
Can anyone help me to solve, or write a new code for this situation?
Another question, after I have edited the DataGrid, if I want to save it to the database, I need to execute the DataAdapter's Update Command, right?
So how to code for this? Can anyone show me?
Please guide. Thank you.
Last edited by albertlse; Aug 21st, 2003 at 08:27 PM.
You dotn have to write thant bunch of code for this, just:
Imagine you have columns Net,Quantitiy,and total in mytable. you can use:
mytable.Columns("Total").Expression="Net * Quantity"
I attached a sample for you. put the database in C:\ to work.
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
An unhandled exception of type 'System.Data.EvaluateException' occurred in system.data.dll
Additional information: Cannot perform '*' operation on System.String and System.String.
Its seems that you have defined those fields "Taken" and "NetPrice" as Text in your database. Either change their datatype to Number or try changing them to Number in the expression.
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
But the last part "System.Double" seems to generate error:
'Double' is a type in 'System' and cannot be used as an expression.
How to correct this code?
I want to use this method because, my DataGrid shows those number like 89.50 as 89.5, 95.00 as 95. I want it to be exactly 2 decimal point all the time. And I do not want to change the structure of my Access Database file.
That changes the datatype of your dataset and you can have the database type remained as text. But I am not sure that wont cause you a problem.
Try modifying your expression. Some functions are valid in the expression. Try to see if CInt, or Cdbl are valid in the expression or not.
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
i think i will change the datatype in Access Database file. because my database has data in it. and VB .NET does not allow me to change via VB coding. anyway thanks for your guidance.