Results 1 to 12 of 12

Thread: Datagridview auto calculate columns.

Threaded View

  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.

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