Results 1 to 12 of 12

Thread: Datagridview auto calculate columns.

  1. #1

    Thread Starter
    Addicted Member coolwater's Avatar
    Join Date
    Dec 2004
    Location
    philippines
    Posts
    215

    Question Datagridview auto calculate columns.

    Need Help. I have a Datagridview that have the following columns.

    1. ID
    2. Products
    3. Qty
    4. UnitPrice
    5. 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.

  2. #2
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Music city, U.S.A.
    Posts
    1,253

    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

  3. #3

    Thread Starter
    Addicted Member coolwater's Avatar
    Join Date
    Dec 2004
    Location
    philippines
    Posts
    215

    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',

  4. #4
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Music city, U.S.A.
    Posts
    1,253

    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

  5. #5
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

    Re: Datagridview auto calculate columns.

    Need to see the code for "GetOtherFeeAmount"

  6. #6

    Thread Starter
    Addicted Member coolwater's Avatar
    Join Date
    Dec 2004
    Location
    philippines
    Posts
    215

    Re: Datagridview auto calculate columns.

    Quote Originally Posted by dolot View Post
    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.

  7. #7
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

    Re: Datagridview auto calculate columns.

    Quote Originally Posted by coolwater View Post
    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.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Datagridview auto calculate columns.

    If the dgv is bound to a datatable, you can use an expression to autocalculate the Total field

  9. #9

    Thread Starter
    Addicted Member coolwater's Avatar
    Join Date
    Dec 2004
    Location
    philippines
    Posts
    215

    Re: Datagridview auto calculate columns.

    Quote Originally Posted by wes4dbt View Post
    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

  10. #10

    Thread Starter
    Addicted Member coolwater's Avatar
    Join Date
    Dec 2004
    Location
    philippines
    Posts
    215

    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.

  11. #11
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

    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

  12. #12
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Music city, U.S.A.
    Posts
    1,253

    Re: Datagridview auto calculate columns.

    Quote Originally Posted by wes4dbt View Post
    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
  •  



Click Here to Expand Forum to Full Width