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:
Quote:
"An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dll
Additional information: The ListManager's position must be equal to rowNum."
The error occurs at the last line:
Code:
DataGrid.Item(intPreviousRow, intColumn + 2) = dblTotalPrice
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.