📄 editform.vb
字号:
Imports System.Xml.Serialization
Imports System.IO
Public Class frmEdit
Private blnIsLoaded As Boolean
Private blnUseBindingSource As Boolean '= True
Private objNwind As Northwind
Private Const strIEFile As String = "\Program Files\Internet Explorer\Iexplore.exe"
Private strPath As String
Private strFile As String
Private alCustomers As New ArrayList
Private alOrders As New ArrayList
Private alOrder_Details As New ArrayList
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If File.Exists("..\Output.xml") Then
File.Delete("..\Output.xml")
End If
End Sub
Private Sub btnLoadData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadData.Click
Dim lngTicks As Long = Now.Ticks
Dim intRows As Integer
txtTime.Text = ""
blnIsLoaded = False
Me.Cursor = Cursors.WaitCursor
'Hydrate the NorthwindDS object by deserializing it
objNwind = New Northwind
Dim xsInput As New XmlSerializer(objNwind.GetType)
Dim fsInput As New FileStream("..\Northwind.xml", FileMode.Open)
objNwind = CType(xsInput.Deserialize(fsInput), Northwind)
fsInput.Close()
'ArrayLists work as DataSource, but no benefit
'alCustomers = ArrayList.Adapter(objNwind.Customers)
'alOrders = ArrayList.Adapter(objNwind.Orders)
'alOrder_Details = ArrayList.Adapter(objNwind.Order_Details)
If chkUseBindingSource.Checked Then
With NorthwindCustomersBindingSource
.Clear()
Application.DoEvents()
'.DataSource = alCustomers
.DataSource = objNwind
.DataMember = "Customers"
With NorthwindCustomersDataGridView
.DataSource = NorthwindCustomersBindingSource
.DataMember = ""
End With
intRows += .Count
End With
Application.DoEvents()
With NorthwindOrdersBindingSource
.Clear()
Application.DoEvents()
'.DataSource = alOrders
.DataSource = objNwind
.DataMember = "Orders"
'Unfortunately, not supported on arrays
'.Sort = "OrderID DESC"
'.Filter = "CustomerID = '" + Me.NorthwindCustomersBindingSource.Item(0).ToString + "'"
With NorthwindOrdersDataGridView
.DataSource = NorthwindOrdersBindingSource
.DataMember = ""
End With
intRows += .Count
End With
Application.DoEvents()
With NorthwindOrder_DetailsBindingSource
.Clear()
Application.DoEvents()
'.DataSource = alOrder_Details
.DataSource = objNwind
.DataMember = "Order_Details"
'.Sort = "ProductID"
'.Filter = "OrderID = " + Me.NorthwindOrdersBindingSource.Item(0).ToString
With NorthwindOrder_DetailsDataGridView
.DataSource = NorthwindOrder_DetailsBindingSource
.DataMember = ""
End With
intRows += .Count
End With
Else
With NorthwindCustomersDataGridView
.DataSource = objNwind
.DataMember = "Customers"
Application.DoEvents()
intRows += .Rows.Count
End With
With NorthwindOrdersDataGridView
.DataSource = objNwind
.DataMember = "Orders"
Application.DoEvents()
intRows += .Rows.Count
End With
With NorthwindOrder_DetailsDataGridView
.DataSource = objNwind
.DataMember = "Order_Details"
Application.DoEvents()
intRows += .Rows.Count
End With
End If
Me.Cursor = Cursors.Default
blnIsLoaded = True
lngTicks = Now.Ticks - lngTicks
txtTime.Text = Format(lngTicks / 10000000, "0.00")
End Sub
Private Sub NorthwindCustomersBindingSource_CurrentChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles NorthwindCustomersBindingSource.CurrentChanged
If blnIsLoaded And blnUseBindingSource Then
With NorthwindCustomersBindingSource
Dim strCustomerID As String = .Item(0).ToString
With NorthwindOrdersDataGridView
Dim intRow As Integer
For intRow = 0 To .Rows.Count - 1
If .Rows(intRow).Cells(1).ToString = strCustomerID Then
.Rows(intRow).Visible = True
Else
.Rows(intRow).Visible = False
End If
Next
'.Sort(.Columns(0), System.ComponentModel.ListSortDirection.Descending)
End With
End With
End If
End Sub
Private Sub NorthwindOrdersBindingSource_CurrentChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles NorthwindOrdersBindingSource.CurrentChanged
If blnIsLoaded And blnUseBindingSource Then
With NorthwindOrdersBindingSource
Dim strOrderID As String = .Item(0).ToString
With NorthwindOrder_DetailsDataGridView
Dim intRow As Integer
For intRow = 0 To .Rows.Count - 1
If .Rows(intRow).Cells(0).ToString = strOrderID Then
.Rows(intRow).Visible = True
Else
.Rows(intRow).Visible = False
End If
Next
'.Sort(.Columns(0), System.ComponentModel.ListSortDirection.Descending)
End With
End With
End If
End Sub
Private Sub btnSaveData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveData.Click
'End the edits
If chkUseBindingSource.Checked Then
NorthwindCustomersBindingSource.EndEdit()
NorthwindOrdersBindingSource.EndEdit()
NorthwindOrder_DetailsBindingSource.EndEdit()
Else
NorthwindCustomersDataGridView.EndEdit(DataGridViewDataErrorContexts.Commit)
NorthwindOrdersDataGridView.EndEdit(DataGridViewDataErrorContexts.Commit)
NorthwindOrder_DetailsDataGridView.EndEdit(DataGridViewDataErrorContexts.Commit)
End If
'Dehydrate the Northwind object by serializing it to Output.xml
Dim xsOutput As New XmlSerializer(objNwind.GetType)
Dim srOutput As New StreamWriter("..\Output.xml")
xsOutput.Serialize(srOutput, objNwind)
srOutput.Close()
If File.Exists(strIEFile) And File.Exists("..\Output.xml") Then
strPath = Application.StartupPath
strPath = Mid(strPath, 1, strPath.LastIndexOf("\"))
strFile = strPath + "\Output.xml"
Dim strShell As String = """" + strIEFile + """ " + strFile
Shell(strShell, AppWinStyle.NormalFocus)
End If
End Sub
Private Sub NorthwindCustomersDataGridView_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles NorthwindCustomersDataGridView.RowEnter
If blnIsLoaded And Not blnUseBindingSource Then
Dim strCustomerID As String
With NorthwindCustomersDataGridView
strCustomerID = .Rows(e.RowIndex).Cells(0).Value.ToString
End With
With NorthwindOrdersDataGridView
Dim intRow As Integer
Dim alBands As New ArrayList
Dim rowBand As DataGridViewBand
For intRow = 0 To .Rows.Count - 1
rowBand = .Rows(intRow)
If .Rows(intRow).Cells(1).ToString = strCustomerID Then
rowBand.Visible = True
'.Rows(intRow).Visible = True
Else
alBands.Add(rowBand)
'rowBand.Visible = False
'.Rows(intRow).Visible = False
End If
Next
For intRow = 0 To alBands.Count - 1
rowBand = CType(alBands(intRow), DataGridViewBand)
'rowBand.Visible = False
Next
End With
End If
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -