⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 editds.vb

📁 wrox出版社的另一套经典的VB2005数据库编程学习书籍,收集了书中源码,郑重推荐,电子书,电子书下载
💻 VB
字号:
Option Explicit On
Option Strict On

Imports System.Data
Imports System.IO

Public Class EditDS
    Private strPath As String
    Private strFile As String
    Private strXML As String
    Private intNewOrder_ID As Integer
    Private Const strIEFile As String = "\Program Files\Internet Explorer\Iexplore.exe"
    Private intOrderID As Integer
    Private blnIsLoaded As Boolean

    '******************************
    'Form and button event handlers
    '******************************

    Private Sub Form_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            'Project folder (not ...\bin folder)
            strPath = Application.StartupPath
            'strPath = Mid(strPath, 1, strPath.LastIndexOf("\"))
            strFile = strPath + "\NorthwindDS.xml"

            'Read the source XML file
            Me.NorthwindDS.EnforceConstraints = False
            Me.NorthwindDS.ReadXml(strFile, Data.XmlReadMode.Auto)
            If Me.NorthwindDS.HasErrors Then
                Stop
            End If

            'Workaround to fix up DataGridViews display on loading (Beta 2 bug)
            bindingNavigatorMoveLastItem.PerformClick()
            Application.DoEvents()
            bindingNavigatorMoveFirstItem.PerformClick()

            'Workaround to remove empty (ghost) Orders records with bogus OrderID values
            With OrdersBindingSource
                .Filter = "CustomerID IS NULL"
                While .Count > 0
                    .RemoveAt(.Count - 1)
                End While
                .RemoveFilter()
                .Sort = "OrderID DESC"
            End With

            Me.NorthwindDS.AcceptChanges()

            'Set the descending sort order for Orders
            With OrdersDataGridView
                .Sort(.Columns(0), System.ComponentModel.ListSortDirection.Descending)
            End With

            'Sort Order Details on ProductID
            With Order_DetailsDataGridView
                .Sort(.Columns(1), System.ComponentModel.ListSortDirection.Ascending)
            End With

            'Show the display buttons if IE is in normal location
            If File.Exists(strIEFile) Then
                btnShowDocument.Visible = True
                btnShowSchema.Visible = True
            End If

            blnIsLoaded = True
        Catch exc As Exception
            MsgBox(exc.Message)
        End Try
    End Sub

    Private Sub btnSaveData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveData.Click
        'Save changes, if edited, and reload the data
        CustomersBindingSource.EndEdit()
        OrdersBindingSource.EndEdit()
        Order_DetailsBindingSource.EndEdit()
        With Me.NorthwindDS
            If .HasChanges Then
                Me.Cursor = Cursors.WaitCursor
                'Save a DiffGram for reference
                Dim strDGFile As String = strPath + "\NorthwindDG.xml"
                .WriteXml(strDGFile, Data.XmlWriteMode.DiffGram)

                'Save the edited document
                .AcceptChanges()
                .WriteXml(strFile, Data.XmlWriteMode.IgnoreSchema)

                'Clear DataSet and reload the data to prove changes saved
                blnIsLoaded = False
                .Clear()
                Form_Load(Nothing, Nothing)
                Me.Cursor = Cursors.Default
            Else
                Dim strMsg As String = "There are no updates to save."
                MsgBox(strMsg, , "Save Request Without Changes")
            End If
        End With
    End Sub

    Private Sub btnShowDocument_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowDocument.Click
        Dim strShell As String = """" + strIEFile + """ " + strFile
        Shell(strShell, AppWinStyle.NormalFocus)
    End Sub

    Private Sub btnShowSchema_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowSchema.Click
        Shell("""" + strIEFile + """ " + Replace(strFile, ".xml", ".xsd"), AppWinStyle.NormalFocus)
    End Sub

    Private Sub OrdersDataGridView_DefaultValuesNeeded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles OrdersDataGridView.DefaultValuesNeeded
        If blnIsLoaded Then
            'Set the default values
            With CustomersDataGridView
                'CustomerID
                If .SelectedRows.Count > 0 Then
                    e.Row.Cells(1).Value = .SelectedRows(0).Cells(0).Value
                End If
                'EmployeeID
                e.Row.Cells(2).Value = 2
                'OrderDate
                e.Row.Cells(3).Value = Today
                'RequiredDate
                e.Row.Cells(4).Value = Today.AddDays(14)
                'ShipBy
                e.Row.Cells(6).Value = 2
                'Freight
                e.Row.Cells(7).Value = 0D
                'ShipTos
                If .SelectedRows.Count > 0 Then
                    e.Row.Cells(8).Value = .SelectedRows(0).Cells(1).Value
                    e.Row.Cells(9).Value = .SelectedRows(0).Cells(4).Value
                    e.Row.Cells(10).Value = .SelectedRows(0).Cells(5).Value
                    e.Row.Cells(11).Value = .SelectedRows(0).Cells(6).Value
                    e.Row.Cells(12).Value = .SelectedRows(0).Cells(7).Value
                    e.Row.Cells(13).Value = .SelectedRows(0).Cells(8).Value
                End If
            End With
        End If
    End Sub

    Private Sub Order_DetailsDataGridView_DefaultValuesNeeded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles Order_DetailsDataGridView.DefaultValuesNeeded
        With OrdersDataGridView
            If .SelectedRows.Count > 0 Then
                'OrderID
                e.Row.Cells(0).Value = .SelectedRows(0).Cells(0).Value
            End If
            'ProductID
            e.Row.Cells(1).Value = e.Row.Index + 1
            'UnitPrice
            e.Row.Cells(2).Value = 10 + e.Row.Index
            'Quantity
            e.Row.Cells(3).Value = 10
            'Discount
            e.Row.Cells(4).Value = 0.075
        End With
    End Sub

    Private Sub bindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bindingNavigatorSaveItem.Click
        'Alternate SaveData button
        btnSaveData.PerformClick()
    End Sub
End Class

⌨️ 快捷键说明

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