TelerikDeveloper.com

TelerikDeveloper.com

sgray128 2/22/2017 1:43:24 PM

Form with Add New, Delete, limited Update

This is template code behind for a form with a GridView that supports AddNew, Delete, and limited Update. The form updates on close.


Imports System.ComponentModel
Imports Telerik.WinControls.UI
 
Public Class PickupEdit
    Dim mstrTruckPO As String
    Dim mintTruckPODetailID As Int32
    Dim mstrItemNumber As String = ""
    Dim mstrUofM As String
    Dim mintDecPlaceQty As Int16
    Dim mstrSopnumber As String
    Dim mintSoptype As Int16
    Dim mintLineItemSeq As Int32
    Dim mstrVendorID As String
    Dim mstrSellingUofM As String
 
    Sub New(strTruckPO As String, intTruckPODetailID As Int32, strItemNumber As String, strUofM As String, strSopnumber As String, intSoptype As Int16, intLineItemSeq As Int32, strVendorID As String)
 
        ' This call is required by the designer.
        InitializeComponent()
 
        ' Add any initialization after the InitializeComponent() call.
        mstrTruckPO = strTruckPO
        mintTruckPODetailID = intTruckPODetailID
        mstrItemNumber = strItemNumber
        mstrUofM = strUofM
        mstrSopnumber = strSopnumber
        mintSoptype = intSoptype
        mintLineItemSeq = intLineItemSeq
        mstrVendorID = strVendorID
    End Sub
    Private Sub PickupEdit_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim strError As String = ""
        'strError = String.Format("Param: {0}",Param)
        Try
            Me.txtItemNumber.Text = mstrItemNumber
            Me.txtTruckPO.Text = mstrTruckPO
            Me.txtItemNumber.Enabled = False
            Me.txtTruckPO.Enabled = False
 
            Dim oIV00101 As New IV00101(mstrItemNumber, App.Database)
            mintDecPlaceQty = oIV00101.DECPLQTY
            mstrSellingUofM = oIV00101.SELNGUOM
 
            If mstrSellingUofM.Trim = "" Then
                Throw New Exception(String.Format("Critical error opening PickupEdit, unable to find the selling UofM for {0}", mstrItemNumber))
            End If
            SetupGrid()
            BindGrid()
        Catch ex As Exception
            ErrorHandler.globalErrorHandler(ex, strError, True)
            Me.Close()
        End Try
    End Sub
    Sub SetupGrid()
        Dim strError As String = ""
        Try
 
            'create the new grid object
            Dim oTelerikGrid As New FPTelerikCommon.TelerikGrid
 
            Dim strFormat As String = String.Format("{{0:f{0}}}", mintDecPlaceQty - 1)
 
            'add new columns by type and name to the grid...verified in SQL server
            Dim oDT As DataTable = DynData.SPs.FP_POP10110_SEL_byITEMNMBR("", mstrItemNumber, mstrUofM, appUser.Db).getTable
            Me.RadGridView1.Columns.Add(oTelerikGrid.CreateGridViewComboBoxColumn("Pickup", oDT, "PICKUP", "PICKUP", "PICKUP", 80))
            Dim col As GridViewComboBoxColumn = CType(Me.RadGridView1.Columns(0), GridViewComboBoxColumn)
            col.AutoCompleteMode = AutoCompleteMode.SuggestAppend
            col.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDown
 
            Me.RadGridView1.Columns.Add(oTelerikGrid.createGridViewTextBoxColumn("PO Number", "PONumber", 11, 100, True, True))
            Me.RadGridView1.Columns.Add(oTelerikGrid.createGridViewDecimalColumn("Quantity", "Quantity", strFormat, 100, True, False, 99999999, mintDecPlaceQty - 1))
 
            'not visible
            Me.RadGridView1.Columns.Add(oTelerikGrid.createGridViewTextBoxColumn("", "ORD", 11, 100, False, False))
            Me.RadGridView1.Columns.Add(oTelerikGrid.createGridViewTextBoxColumn("", "RowID", 11, 100, False, False))
 
            'set the grid properties
            Me.RadGridView1.EnableGrouping = False
            Me.RadGridView1.AllowAddNewRow = True
            Me.RadGridView1.AllowEditRow = True
            Me.RadGridView1.AllowDeleteRow = True
            Me.RadGridView1.EnableFiltering = False
            Me.RadGridView1.MasterTemplate.EnableSorting = True
        Catch ex As Exception
            ErrorHandler.globalErrorHandler(ex, strError, True)
        End Try
 
    End Sub
 
    Sub BindGrid()
        Dim strError As String = ""
        Try
            Me.RadGridView1.DataSource = DynData.SPs.FP_POP10110SOR_SEL_pickup(mintTruckPODetailID, appUser.Db).getTable
 
        Catch ex As Exception
            ErrorHandler.globalErrorHandler(ex, strError, True)
        End Try
 
    End Sub
 
    Private Sub RadGridView1_CellEndEdit(sender As Object, e As GridViewCellEventArgs) Handles RadGridView1.CellEndEdit
        Dim strError As String = ""
        'strError = String.Format("Param: {0}",Param)
        Try
            'PICKUP is only editable in the new row (see the CellBeginEdit)
            Select Case e.Column.Name.ToUpper
                Case "PICKUP"
                    Dim strPickup As String = Me.RadGridView1.CurrentRow.Cells("pickup").Value
                    If strPickup = "" Then
                        'Exit Sub
                    End If
 
                    Dim oDT As DataTable = SPs.FP_POP10110_SEL_byITEMNMBR("", mstrItemNumber, mstrUofM, App.Database).getTable
                    For Each oRow As DataRow In oDT.Rows
                        If oRow("pickup") = strPickup Then
                            Me.RadGridView1.CurrentRow.Cells("PONumber").Value = oRow("PONumber")
                            Me.RadGridView1.CurrentRow.Cells("ORD").Value = oRow("ORD")
                            Me.RadGridView1.CurrentRow.Cells("Quantity").Value = 0
                            Exit For
                        End If
                    Next
                Case "QUANTITY"
                    'this is a data row, not a new row
 
                    Dim decQuantity As Decimal = FPCommon.BL.GetRoundedQuantity(mstrItemNumber, Me.RadGridView1.CurrentRow.Cells("Quantity").Value, mstrSellingUofM, BL.RoundDirectionType.up)
                    If decQuantity <> Me.RadGridView1.CurrentRow.Cells("Quantity").Value Then
                        Me.RadGridView1.CurrentRow.Cells("Quantity").Value = decQuantity
                    End If
 
            End Select
 
        Catch ex As Exception
            ErrorHandler.globalErrorHandler(ex, strError, True)
        End Try
    End Sub
 
    Private Sub RadGridView1_CellBeginEdit(sender As Object, e As GridViewCellCancelEventArgs) Handles RadGridView1.CellBeginEdit
        Dim strError As String = ""
        'strError = String.Format("Param: {0}",Param)
        Try
            If TypeOf (e.Row) Is GridViewNewRowInfo Then
 
            End If
 
            If TypeOf (e.Row) Is GridViewDataRowInfo Then
                'if this is not the new row, only allow the quantity to be edited
                Select Case e.Column.Name.ToUpper
                    Case "QUANTITY"
                    Case Else
                        e.Cancel = True
                End Select
            End If
 
 
        Catch ex As Exception
            ErrorHandler.globalErrorHandler(ex, strError, True)
        End Try
    End Sub
    Sub Save()
        Dim strError As String = ""
        'strError = String.Format("Param: {0}",Param)
        Try
            Dim oDT As DataTable = Me.RadGridView1.DataSource
            Dim oDTchanges As DataTable = oDT.GetChanges
 
            If Not IsNothing(oDTchanges) Then
                For Each oRow As DataRow In oDTchanges.Rows
                    Select Case oRow.RowState
                        Case DataRowState.Added
                            'get the new data
                            Dim strPONumber As String = oRow("ponumber")
                            Dim intORD As Int32 = oRow("ord")
                            Dim decQuantity As Decimal = oRow("quantity")
 
                            'data access code to save new rows
                            SPs.FP_POP10110SOR_Merge(0, "TPO", mintTruckPODetailID, mstrSopnumber, mintSoptype, mintLineItemSeq, strPONumber, intORD, decQuantity, mstrVendorID, App.Database).execute()
                        Case DataRowState.Deleted
                            Dim intRowID As Int32 = oRow("RowID", DataRowVersion.Original)
                            SPs.FP_POP10110SOR_DEL_byID(intRowID, App.Database).execute()
                        Case DataRowState.Modified
                            'get the changed data
                            Dim intRowID As Int32 = oRow("RowID")
 
                            Dim strPONumber As String = oRow("ponumber")
                            Dim intORD As Int32 = oRow("ord")
                            Dim decQuantity As Decimal = oRow("quantity")
                            Debug.WriteLine(String.Format("save {0}", decQuantity))
 
                            'data access code to update the row
                            SPs.FP_POP10110SOR_Merge(intRowID, "TPO", mintTruckPODetailID, mstrSopnumber, mintSoptype, mintLineItemSeq, strPONumber, intORD, decQuantity, mstrVendorID, App.Database).execute()
                    End Select
                Next
 
                oDT.AcceptChanges()
                BindGrid()
            End If
        Catch ex As Exception
            ErrorHandler.globalErrorHandler(ex, strError, True)
        End Try
    End Sub
 
 
    Private Sub PickupEdit_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
        Me.RadGridView1.EndEdit()
        Save()
    End Sub
End Class