Private Sub BtnLoadXMLData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles BtnLoadXMLData.Click
' Instantiate a DataSet type alarmlist
DS = New alarmlist()
' Load the XML data from file AlarmList.xml in the source code folder. Note that the program EXE resides
' in the bin subfolder
DS.ReadXml("../AlarmList.xml")
' Bind the Datagrid DataSource to this new DataSet table alarm
' DataGrid1.DataSource = DS.alarm
' Create a Dataview from DS
DV1 = New System.Data.DataView(DS.alarm)
' Sort alarms by priority, then datetime
' DESC stands for descending order,i.e. biggest on top
DV1.Sort = "priority DESC, datetime DESC"
' Bind the Datagrid DataSource to Dataview
DataGrid1.DataSource = DV1
AddCustomDataTableStyle()
' Display the number of alarms in each priority
DisplayTotal()
End Sub
|
Chắc bạn đã để ý thấy thay vì iterate qua mỗi record để đếm con số alarms thuộc priority 1,2 hay 3, ta đã dùng ba Dataviews để filter ra alarms thuộc ba priorities khác nhau rồi lấy trị số Count của mỗi Dataview. Đây là lối lập trình dựa vào những gì có sẵn càng nhiều càng tốt để tránh tạo ra bugs.
Private Sub DisplayTotal() ' Create a Dataview object from table DS.alarm Dim DVP1 As New System.Data.DataView(DS.alarm) ' Apply filter DVP1.RowFilter = "priority = 1" ' Display Count of records in this Dataview NumPrio1.Text = "Prio1: " & DVP1.Count.ToString Dim DVP2 As New System.Data.DataView(DS.alarm) DVP2.RowFilter = "priority = 2" NumPrio2.Text = "Prio2: " & DVP2.Count.ToString Dim DVP3 As New System.Data.DataView(DS.alarm) DVP3.RowFilter = "priority = 3" NumPrio3.Text = "Prio3: " & DVP3.Count.ToString NumTotal.Text = "Total: " & DS.alarm.Rows.Count.ToString Dim bmb As BindingManagerBase = Me.BindingContext(DataGrid1.DataSource, DataGrid1.DataMember) NumDisplayed.Text = "Displayed: " & bmb.Count.ToString End Sub
Dim bmb As BindingManagerBase = Me.BindingContext(DataGrid1.DataSource, DataGrid1.DataMember) NumDisplayed.Text = "Displayed: " & bmb.Count.ToString |
Bạn có thể chay chương trình và bấm các nút vừa mới thêm vào để xem các alarms được filtered như thế nào.
Private Sub Btn1and2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Btn1and2.Click DV1.RowFilter = "priority < 3" Dim bmb As BindingManagerBase = Me.BindingContext(DataGrid1.DataSource, DataGrid1.DataMember) NumDisplayed.Text = "Displayed: " & bmb.Count.ToString End Sub Private Sub Btn1Only_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Btn1Only.Click DV1.RowFilter = "priority = 1" Dim bmb As BindingManagerBase = Me.BindingContext(DataGrid1.DataSource, DataGrid1.DataMember) NumDisplayed.Text = "Displayed: " & bmb.Count.ToString End Sub Private Sub BtnAllAlarms_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles BtnAllAlarms.Click DV1.RowFilter = "" Dim bmb As BindingManagerBase = Me.BindingContext(DataGrid1.DataSource, DataGrid1.DataMember) NumDisplayed.Text = "Displayed: " & bmb.Count.ToString End Sub
Mỗi khi user right click lên một hàng alarm, ContextMenu1 sẽ hiển thị chỉ những menuCommands thích hợp với tình huống. Tức là nếu alarm chưa được acknowledged thì mới có menuCommand Acknowledge, khi alarm chưa bị isolated thì mới có menuCommand Isolate, nếu đã bị isolated rồi thì chỉ có MenuCommand Enable.
Khi User click một trong các Popup menu commands ta thay đổi các boolean value Ackn hay Isolate và viết xuống AlarmList.xml data file như sau:
' Variable used to store selected DataRowView Dim drv As DataRowView Private Sub DataGrid1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _ Handles DataGrid1.MouseDown ' Only proceed when Mouse Right Button was clicked If e.Button <> MouseButtons.Right Then Exit Sub ' Typecast sender to DataGrid data type. myGrid is actually DataGrid1 Dim myGrid As DataGrid = CType(sender, DataGrid) ' Declare a HitTestInfo variable Dim hti As DataGrid.HitTestInfo ' Obtain the info about Mouse location hti = myGrid.HitTest(e.X, e.Y) ' Only proceed when a Cell was hit If hti.Type = DataGrid.HitTestType.Cell Then Try ' Obtain BindingManagerBase of DataGrid1 Dim bmb As BindingManagerBase = Me.BindingContext(myGrid.DataSource, myGrid.DataMember) ' Position at DataRowView corresponding to the physical row that was hit bmb.Position = hti.Row ' Store the found DataRowView in temporary variable drv drv = bmb.Current ' Display description of the alarm line as a feedback Label1.Text = drv("description") If Not (drv Is Nothing) Then ' Only display the MenuCommands that are appropriate to this context Dim ctx As DataRow = drv.Row If Not (ctx Is Nothing) Then If drv("ackn") = True Then mnuAckn.Visible = False Else ' Only display menuCommand Ackn when alarm is not yet acknowledged mnuAckn.Visible = True End If If drv("isolate") = True Then ' If alarm is already isolated then only display MenuCommand Enable mnuIsolate.Visible = False mnuEnable.Visible = True Else mnuIsolate.Visible = True mnuEnable.Visible = False End If ' Popup context menu ContextMenu1.Show(myGrid, New Point(e.X, e.Y)) End If End If Catch ex As Exception MessageBox.Show(ex.ToString()) End Try End If End Sub
Private Sub mnuAckn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAckn.Click
If Not drv Is Nothing Then
drv("ackn") = True
UpdatePoint()
End If
End Sub
Private Sub mnuIsolate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuIsolate.Click
If Not drv Is Nothing Then
drv("isolate") = True
UpdatePoint()
End If
End Sub
Private Sub mnuEnable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuEnable.Click
If Not drv Is Nothing Then
drv("isolate") = False
UpdatePoint()
End If
End Sub
Private Sub UpdatePoint()
' Accept the changes in DataSet
DS.AcceptChanges()
' Write updated data to XML file to persist the information
DS.WriteXml("..\AlarmList.xml")
End Sub
|
Để Edit cái schema mới nầy, bạn hãy doubleclick lên tên operatorlist.xsd trong Solution Explorer. Một khung vàng nhạt còn trống sẽ hiện ra ở giữa. Bạn hãy drag icon E Element từ XML Schema Toolbox (nằm bên trái ) vào khung vàng nhạt. Hãy click lên chữ Element1 (nằm bên phải chữ E) trong hình mới tạo ra và sửa nó thành operator như trong hình dưới đây:
Kế đó drag icon A Attribute từ XML Schema Toolbox vào drop nó ngay trên hàng nằm dưới Element operator. Sửa tên Attribute đó thành operatorid. Click bên phải chữ string của operatorid để chọn datatype integer từ ComboBox. Lập lại cùng những thao tác ấy cho Attributes username, password và level. Click bên phải chữ string của level để chọn datatype integer từ ComboBox.
Nếu bây giờ bạn click Tab XML phía dưới của khung vàng nhạt bạn sẽ thấy mã nguồn XML của Schema operatorlist.xsd như sau:

|
Vovisoft © 2000. All rights reserved. | ||||
|
Last Updated: 30 th7 2002 |
||||