-
Nov 6th, 2014, 11:54 AM
#1
Thread Starter
Addicted Member
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.
Last edited by coolwater; Nov 6th, 2014 at 01:16 PM.
-
Nov 6th, 2014, 01:02 PM
#2
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.)
I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
My war with a browser-redirect trojan
-
Nov 6th, 2014, 01:13 PM
#3
Thread Starter
Addicted Member
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',
-
Nov 6th, 2014, 01:25 PM
#4
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...
I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
My war with a browser-redirect trojan
-
Nov 6th, 2014, 01:42 PM
#5
Re: Datagridview auto calculate columns.
Need to see the code for "GetOtherFeeAmount"
-
Nov 6th, 2014, 01:48 PM
#6
Thread Starter
Addicted Member
Re: Datagridview auto calculate columns.
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.
-
Nov 6th, 2014, 01:52 PM
#7
Re: Datagridview auto calculate columns.
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.
-
Nov 6th, 2014, 02:23 PM
#8
Re: Datagridview auto calculate columns.
If the dgv is bound to a datatable, you can use an expression to autocalculate the Total field
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 6th, 2014, 10:57 PM
#9
Thread Starter
Addicted Member
Re: Datagridview auto calculate columns.
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
-
Nov 6th, 2014, 11:06 PM
#10
Thread Starter
Addicted Member
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.
-
Nov 7th, 2014, 02:26 AM
#11
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
-
Nov 7th, 2014, 07:52 AM
#12
Re: Datagridview auto calculate columns.
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.
I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
My war with a browser-redirect trojan
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|