salesordersbe.vb

来自「wrox出版社的另一套经典的VB2005数据库编程学习书籍,收集了书中源码,郑重」· VB 代码 · 共 380 行 · 第 1/2 页

VB
380 行
字号
Imports System.Web.Services.protocols
Imports System.Collections
Imports System.Collections.Generic
Imports System.ComponentModel
Public Class SalesOrdersBE

    Private wsOrders As New WSOrdersBE.WSOrdersBE
	Private blnIsLoading As Boolean
	Private blnIsLoaded As Boolean
    Private lstDetailsCopy As New BindingList(Of WSOrdersBE.OrderDetail)

	'*******************************
	'DataGridView loading procedures
	'*******************************

	Private Sub btnLoadGrids_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadGrids.Click
		blnIsLoading = True
		Me.Cursor = Cursors.WaitCursor
        Dim objOrders() As WSOrdersBE.SalesOrder
		Dim lngTicks As Long = Now.Ticks
		Try
			If optGetTopOrders.Checked Then
				If CType(txtTop.Text, Integer) > 25 Then
					ResetAutoSizeColumns(True)
				Else
					ResetAutoSizeColumns(False)
                End If
                objOrders = wsOrders.GetTopSalesOrders(CType(txtTop.Text, Integer))
			ElseIf optGetCustOrders.Checked Then
				ResetAutoSizeColumns(False)
				txtCustID.Text = txtCustID.Text.ToUpper
				objOrders = wsOrders.GetSalesOrdersByCustomerID(txtCustID.Text)
			Else
				ResetAutoSizeColumns(True)
				objOrders = wsOrders.GetAllSalesOrders
			End If
			lngTicks = Now.Ticks - lngTicks
			txtData.Text = Format(lngTicks / 10000000, "#0.000")
            Application.DoEvents()
            Dim intRow As Integer
            lngTicks = Now.Ticks
            SalesOrderBindingSource.Clear()
            SalesOrderDetailsBindingSource.Clear()
            'Suspend/ResumeLayout for improved performance
            SalesOrderDataGridView.SuspendLayout()
            With SalesOrderBindingSource
                'Application.DoEvents()
                Dim lstDetails As New BindingList(Of WSOrdersBE.OrderDetail)
                'lstDetails.Clear()
                For intRow = 0 To objOrders.Length - 1
                    'Add the SalesOrders object
                    .Add(objOrders(intRow))

                    Dim objDtls As WSOrdersBE.OrderDetail()
                    objDtls = objOrders(intRow).OrderDetails
                    Dim intItem As Integer
                    For intItem = 0 To objDtls.Length - 1
                        lstDetails.Add(objOrders(intRow).OrderDetails(intItem))
                        'Populate the copy
                        lstDetailsCopy.Add(objOrders(intRow).OrderDetails(intItem))
                    Next
                Next
                .AllowNew = True
                With SalesOrderDetailsBindingSource
                    'Add the OrderDetails
                    .DataMember = ""
                    .DataSource = lstDetails
                    .AllowNew = False
                End With
            End With
            SalesOrderDataGridView.ResumeLayout()
            SalesOrderDataGridView.Rows(0).Selected = True
            Application.DoEvents()
            lngTicks = Now.Ticks - lngTicks
            txtGrids.Text = Format(lngTicks / 10000000, "#0.000")
            BindingNavigatorSaveItem.Enabled = False
            BindingNavigatorDeleteItem.Enabled = False
            BindingNavigatorAddNewItem.Enabled = True
        Catch excSoap As SoapException
            MsgBox(excSoap.Message, MsgBoxStyle.Information, "SOAP Exception")
        Catch excOther As Exception
            MsgBox(excOther.Message, MsgBoxStyle.Information, "Other Exception (Not SOAP)")
        Finally
            Me.Cursor = Cursors.Default
            blnIsLoading = False
            blnIsLoaded = True
        End Try
    End Sub

    Private Sub ResetAutoSizeColumns(ByVal blnReset As Boolean)
        'Autosizing columns causes a performance hit on large loads
        Dim intCol As Integer
        With SalesOrderDataGridView
            For intCol = 0 To .Columns.Count - 1
                .Columns(intCol).DefaultCellStyle.WrapMode = DataGridViewTriState.False
                .Columns(intCol).SortMode = DataGridViewColumnSortMode.NotSortable
                If blnReset Then
                    .Columns(intCol).AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader
                Else
                    If intCol > 7 Then
                        .Columns(intCol).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
                    Else
                        .Columns(intCol).AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader
                    End If
                End If
            Next
        End With
    End Sub

    Private Sub SalesOrderDataGridView_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles SalesOrderDataGridView.SelectionChanged
        'Display OrderDetails for the selected Order and enable editing the grids
        With SalesOrderDataGridView
            If .SelectedRows.Count > 0 Then
                Dim intSelRow As Integer = .SelectedRows(0).Index
                Dim intOrderID As Integer = CType(.SelectedRows(0).Cells(0).Value, Integer)
                If intOrderID > 0 Then
                    blnIsLoading = True
                    Dim lstDetails As New BindingList(Of WSOrdersBE.OrderDetail) 'or ArrayList
                    Dim lngTicks As Long = Now.Ticks
                    'Copy the related rows to lstDetails
                    With lstDetailsCopy
                        Dim intRow As Integer
                        Dim blnFound As Boolean
                        For intRow = 0 To .Count - 1
                            If .Item(intRow).OrderID = intOrderID Then
                                lstDetails.Add(.Item(intRow))
                                blnFound = True
                            Else
                                If blnFound Then
                                    blnFound = False
                                    Exit For
                                End If
                            End If
                        Next
                    End With
                    lngTicks = Now.Ticks - lngTicks
                    txtData.Text = Format(lngTicks / 10000000, "#0.000")
                    lngTicks = Now.Ticks
                    With SalesOrderBindingSource
                        .AllowNew = True
                    End With
                    With SalesOrderDetailsBindingSource
                        'Add the OrderDetails for the order
                        .Clear()
                        .DataMember = ""
                        .DataSource = lstDetails
                        lngTicks = Now.Ticks - lngTicks
                        txtGrids.Text = Format(lngTicks / 10000000, "#0.000")
                        BindingNavigatorSaveItem.Enabled = True
                        BindingNavigatorDeleteItem.Enabled = True
                        'Enable details editing
                        .AllowNew = True
                    End With
                    blnIsLoading = False
                End If
                .Rows(intSelRow).Selected = True
            End If
        End With
    End Sub

    '*********************************************
    'Add, edit, delete and save SalesOrder objects
    '*********************************************

    Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click
        BindingNavigatorSaveItem.Enabled = True
        SalesOrderBindingSource.MoveLast()
        SalesOrderDetailsBindingSource.Clear()
    End Sub

    Private Sub SalesOrderBindingSource_AddingNew(ByVal sender As Object, ByVal e As System.ComponentModel.AddingNewEventArgs) Handles SalesOrderBindingSource.AddingNew
        'Add default values (for convenience)
        If blnIsLoaded Then
            Dim objNew As New WSOrdersBE.SalesOrder
            With objNew
                .CustomerID = "RATTC"
                .EmployeeID = 2
                .OrderDate = Today
                .RequiredDate = Today.AddDays(14)
                .ShippedDate = #12:00:00 AM#
                .ShipVia = 2
                .Freight = 0D
                .ShipName = "Rattlesnake Canyon Grocery"
                .ShipAddress = "2817 Milton Dr."
                .ShipCity = "Albuquerque"
                .ShipRegion = "NM"
                .ShipPostalCode = "87110"
                .ShipCountry = "USA"
            End With
            e.NewObject = objNew

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?