ordersbe.vb

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

VB
367
字号
Imports System.Web.Services.Protocols

Public Class OrdersBE
    Private wsOrders As New WSOrdersBE.WSOrdersBE
	Private blnIsLoading As Boolean

	'*******************************
	'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.Order
        Dim objDetails() As WSOrdersBE.OrderDetail
		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.GetTopOrders(CType(txtTop.Text, Integer))
				objDetails = wsOrders.GetTopDetails(CType(txtTop.Text, Integer))
			ElseIf optGetCustOrders.Checked Then
				ResetAutoSizeColumns(False)
				txtCustID.Text = txtCustID.Text.ToUpper
				objOrders = wsOrders.GetOrdersByCustomerID(txtCustID.Text)
				objDetails = wsOrders.GetDetailsByCustomerID(txtCustID.Text)
			Else
				ResetAutoSizeColumns(True)
				objOrders = wsOrders.GetAllOrders
				objDetails = wsOrders.GetAllDetails
			End If
			lngTicks = Now.Ticks - lngTicks
			txtData.Text = Format(lngTicks / 10000000, "#0.000")
            Application.DoEvents()
            lngTicks = Now.Ticks
            'Suspend/ResumeLayout improves loading performance
            OrderDetailBindingSource.Clear()
            OrderDataGridView.SuspendLayout()
            Dim intRow As Integer
            With OrderBindingSource
                .Clear()
                Application.DoEvents()
                For intRow = 0 To objOrders.Length - 1
                    .Add(objOrders(intRow))
                Next
                .AllowNew = False
            End With
            OrderDataGridView.ResumeLayout()
            Application.DoEvents()
            OrderDetailDataGridView.SuspendLayout()
            With OrderDetailBindingSource
                For intRow = 0 To objDetails.Length - 1
                    .Add(objDetails(intRow))
                Next
                .AllowNew = False
            End With
            OrderDetailDataGridView.ResumeLayout()
            lngTicks = Now.Ticks - lngTicks
            txtGrids.Text = Format(lngTicks / 10000000, "#0.000")
            BindingNavigatorSaveItem.Enabled = False
            BindingNavigatorDeleteItem.Enabled = False
            With OrderDetailBindingSource
                'Disable details editing
                .AllowNew = False
            End With
            OrderDataGridView.Rows(0).Selected = 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
        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 OrderDataGridView
            For intCol = 0 To .Columns.Count - 1
                .Columns(intCol).SortMode = DataGridViewColumnSortMode.NotSortable
                If blnReset Then
                    .Columns(intCol).AutoSizeMode = DataGridViewAutoSizeColumnMode.None
                Else
                    If intCol > 7 Then
                        .Columns(intCol).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
                    End If
                End If
            Next
        End With
        With OrderDetailDataGridView
            For intCol = 0 To .Columns.Count - 1
                .Columns(intCol).AutoSizeMode = DataGridViewAutoSizeColumnMode.None
            Next
        End With
    End Sub

    Private Sub OrderDataGridView_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles OrderDataGridView.SelectionChanged
        'Display OrderDetails for the selected Order and enable editing the grid
        'Invokes the GetDetailsByOrderID Web Method, which isn't an optimal design
        'SalesOrdersBE's version saves a copy of all details and does a row scan 
        Application.DoEvents()
        With OrderDataGridView
            If .SelectedRows.Count > 0 Then
                Dim intSelRow As Integer = .SelectedRows(0).Index
                Dim objDetails As WSOrdersBE.OrderDetail()
                Dim intOrderID As Integer = CType(.SelectedRows(0).Cells(0).Value, Integer)
                If intOrderID > 0 Then
                    Try
                        Me.Cursor = Cursors.WaitCursor
                        blnIsLoading = True
                        Dim lngTicks As Long = Now.Ticks
                        objDetails = wsOrders.GetDetailsByOrderID(intOrderID)
                        lngTicks = Now.Ticks - lngTicks
                        txtData.Text = Format(lngTicks / 10000000, "#0.000")
                        Application.DoEvents()
                        lngTicks = Now.Ticks
                        Dim intRow As Integer
                        With OrderDetailBindingSource
                            .Clear()
                            For intRow = 0 To objDetails.Length - 1
                                .Add(objDetails(intRow))
                            Next
                            lngTicks = Now.Ticks - lngTicks
                            txtGrids.Text = Format(lngTicks / 10000000, "#0.000")
                            BindingNavigatorDeleteItem.Enabled = True
                            'Enable details editing
                            .AllowNew = True
                        End With
                        With OrderBindingSource
                            .AllowNew = True
                        End With
                        blnIsLoading = False
                    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
                    End Try
                End If
                .Rows(intSelRow).Selected = True
            End If
        End With
    End Sub

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

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

    Private Sub OrderBindingSource_AddingNew(ByVal sender As Object, ByVal e As System.ComponentModel.AddingNewEventArgs) Handles OrderBindingSource.AddingNew
        'Add default values (for convenience)
        Dim objNew As New WSOrdersBE.Order
        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
        BindingNavigatorSaveItem.Enabled = False
    End Sub

⌨️ 快捷键说明

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