Datagridview auto calculate columns.
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.
Re: Datagridview auto calculate columns.
Try using something like this in place of the selected value:
Code:
CType(ComboBox.SelectedValue, DataRowView).Item('whatever index works here.)
Re: Datagridview auto calculate columns.
Thanks for the reply. I got this error. Unable to cast object of type 'System.Int64' to type 'System.Data.DataRowView',
Re: Datagridview auto calculate columns.
OK. So that means that the Combobox.SelectedValue wasn't the issue after all, because the type of the SelectedValue is a long value like you're expecting.
Not sure where the issue is...
Re: Datagridview auto calculate columns.
Need to see the code for "GetOtherFeeAmount"
Re: Datagridview auto calculate columns.
Quote:
Originally Posted by
dolot
OK. So that means that the Combobox.SelectedValue wasn't the issue after all, because the type of the SelectedValue is a long value like you're expecting.
Not sure where the issue is...
BTW: I edited my post above. I noticed I can add new rows with no errors. The error will only occur if I go back to a combobox and change the selection or cancel a new row.
Re: Datagridview auto calculate columns.
Quote:
Originally Posted by
coolwater
BTW: I edited my post above. I noticed I can add new rows with no errors. The error will only occur if I go back to a combobox and change the selection or cancel a new row.
That makes sense, that's when SelectedIndexChanged would be called. GetOtherFeeAmount doesn't seem to like the arguments you send.
Re: Datagridview auto calculate columns.
If the dgv is bound to a datatable, you can use an expression to autocalculate the Total field
Re: Datagridview auto calculate columns.
Quote:
Originally Posted by
wes4dbt
Need to see the code for "GetOtherFeeAmount"
My GetOtherFeeAmount Code:
Code:
Private Function GetOtherFeeAmount(intSLID As Long, intSYID As Long, intSemesterID As Long) As Decimal
Using con As SqlConnection = New SqlConnection(CS)
Dim da As SqlDataAdapter
Dim ds As DataSet
da = New SqlDataAdapter("NET_OtherCharges_GetOtherFeeAmount", con)
da.SelectCommand.CommandType = CommandType.StoredProcedure
da.SelectCommand.Parameters.AddWithValue("@SLID", CLng(intSLID))
da.SelectCommand.Parameters.AddWithValue("@SYID", CLng(intSYID))
da.SelectCommand.Parameters.AddWithValue("@SemesterID", CLng(intSemesterID))
da.SelectCommand.CommandTimeout = 60
ds = New DataSet
da.Fill(ds)
ds.Tables(0).TableName = "OtherFees"
If ds.Tables("OtherFees").Rows.Count > 0 Then
Return ds.Tables("OtherFees").Rows(0).Item("OtherFeeAmount")
Else
Return 0
End If
End Using
End Function
Re: Datagridview auto calculate columns.
Thanks for the reply. My dilema is my UnitPrice column (readonly) is dependent on the combobox column. Everytime the combobox selected value is changed the UnitPrice column is also updated. Somehow straight encoding of data is fine. The problem occurs when I go back and change a combobox value or if I cancel a new row.
Re: Datagridview auto calculate columns.
The error message tells you the problem,
Code:
Conversion from type 'DataRowView' to type 'Long' is not valid
The function GetOtherFeeAmount is expecting all the parameters to be type Long, so one of your parameters is of Type DataRowView. Or it actually could be in GetDefaultSchoolYearOrSemester, I haven't seen that code.
I believe that combobox.Selectedvalue is of Type DataRowView
Re: Datagridview auto calculate columns.
Quote:
Originally Posted by
wes4dbt
I believe that combobox.Selectedvalue is of Type DataRowView
That was my first thought as well, but see where that went in posts 2 & 3.