Need Help. I have a Datagridview that have the following columns.
- ID
- Products
- Qty
- UnitPrice
- Total
The Products column is a combobox. The products combobox when selected will autofill the UnitPrice column. I have subroutine (GetOtherFeeAmount) that will get the UnitPrice from the database according to the item selected from the Products combobox.
The Problem: I get this error "Conversion from type 'DataRowView' to type 'Long' is not valid." The initial first row data entry is ok. I will always get this error everytime I go back and change a combobox selection.
This is where the error occurs highlighted in RED:
Code:
Private Sub ComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim comboBox As ComboBox = CType(sender, ComboBox)
If comboBox IsNot Nothing Then
Dim cdecUnitPrice As Decimal
cdecUnitPrice = GetOtherFeeAmount(GetDefaultSchoolYearOrSemester("School Year"), GetDefaultSchoolYearOrSemester("Semester Name"), comboBox.SelectedValue)
dgvFees("UnitPrice", dgvFees.CurrentRow.Index).Value = cdecUnitPrice
dgvFees("FeeAmount", dgvFees.CurrentRow.Index).Value = CInt(dgvFees("Qty", dgvFees.CurrentRow.Index).Value) * CDec(dgvFees("UnitPrice", dgvFees.CurrentRow.Index).Value)
End If
End Sub
My EditingControlShowing Code:
Code:
Private Sub dgvFees_EditingControlShowing(sender As Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgvFees.EditingControlShowing
If TypeOf e.Control Is DataGridViewComboBoxEditingControl Then
Dim te As DataGridViewComboBoxEditingControl = DirectCast(e.Control, DataGridViewComboBoxEditingControl)
te.AutoCompleteMode = AutoCompleteMode.Suggest
te.AutoCompleteSource = AutoCompleteSource.ListItems
te.SelectedIndex = -1
te.DropDownStyle = ComboBoxStyle.DropDownList
te.FlatStyle = FlatStyle.Flat
End If
''=====check if SLID combobox value is changed =======
If dgvFees.CurrentCell.ColumnIndex = 1 And Not e.Control Is Nothing Then
Dim comboBox As ComboBox
comboBox = CType(e.Control, ComboBox)
If (comboBox IsNot Nothing) Then
RemoveHandler comboBox.SelectedIndexChanged, New EventHandler(AddressOf ComboBox_SelectedIndexChanged)
'Add the event handler.
AddHandler comboBox.SelectedIndexChanged, New EventHandler(AddressOf ComboBox_SelectedIndexChanged)
End If
End If
'=====code below is to control the user to input correct textbox entry
If dgvFees.CurrentCell.ColumnIndex = 2 And Not e.Control Is Nothing Then
Dim tb As TextBox = CType(e.Control, TextBox)
'---add an event handler to the TextBox control---
AddHandler tb.KeyPress, AddressOf TextBox_KeyPress
End If
End Sub
My CellEndEdit Code:
Code:
Private Sub dgvFees_CellEndEdit(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvFees.CellEndEdit
If dgvFees.CurrentCell.ColumnIndex = 2 Then
Dim row As DataGridViewRow = dgvFees.Rows(e.RowIndex)
Dim QtyValue As Decimal = row.Cells("Qty").Value
Dim UnitPriceValue As Decimal = row.Cells("UnitPrice").Value
Dim TotalAmountValue As Decimal = QtyValue * UnitPriceValue
row.Cells("FeeAmount").Value = CDec(TotalAmountValue)
ElseIf dgvFees.CurrentCell.ColumnIndex = 4 Then
txtTotal.Text = GetTotal()
End If
End Sub
Any help will be highly appreciated.