Danh mục

DataGrid (part II)

Số trang: 10      Loại file: pdf      Dung lượng: 189.27 KB      Lượt xem: 15      Lượt tải: 0    
Thư viện của tui

Phí tải xuống: 2,000 VND Tải xuống file đầy đủ (10 trang) 0
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

DataGrid (phần II)Dùng Dataview để Filter và SortThường thường, khi điều khiển trong thời gian thật (real-time control), là Operator, ta muốn các alarms có ưu tiên cao và mới xãy ra nhất được hiển thị trên hết. Đôi khi, ta chỉ muốn thấy các alarm priority 3 (ưu tiên cao nhất) mà thôi. Để thực hiện các việc nầy, ta dùng Dataview Object. Thay vì dùng thẳng table alarm của DataSet alarmlist làm datasource của DataGrid1, ta sẽ dùng một DataView derived from (đến từ) table alarm. Ta có thể Sort (sắp theo thứ tự) các alarms/records theo Priority...
Nội dung trích xuất từ tài liệu:
DataGrid (part II) DataGrid (phần II)Dùng Dataview để Filter và SortThường thường, khi điều khiển trong thời gian thật (real-time control), làOperator, ta muốn các alarms có ưu tiên cao và mới xãy ra nhất được hiển thịtrên hết. Đôi khi, ta chỉ muốn thấy các alarm priority 3 (ưu tiên cao nhất) màthôi. Để thực hiện các việc nầy, ta dùng Dataview Object.Thay vì dùng thẳng table alarm của DataSet alarmlist làm datasource củaDataGrid1, ta sẽ dùng một DataView derived from (đến từ) table alarm. Ta cóthể Sort (sắp theo thứ tự) các alarms/records theo Priority hay áp dụng Filter(sàn lọc) vào DataView để chỉ thấy những thứ gì mình muốn, thí dụ chỉ cóalarms priority 3 thôi.Nên nhớ là nằm đàng sau vẫn là table alarm, nhưng Dataview đóng vai trò cặpkiếng mát màu giúp cho ta thấy những thứ gì và theo cách ta muốn. Mỗi khi tathay một cặp kiếng, ta lại thấy những thứ khác.Dưới đây là Sub BtnLoadXMLData_Click được sửa lại một chút để dùngDataView:Private Sub BtnLoadXMLData_Click(ByVal sender As System.Object, ByVal eAs System.EventArgs) _ HandlesBtnLoadXMLData.Click Instantiate a DataSet type alarmlist DS = New alarmlist() Load the XML data from file AlarmList.xml in the source codefolder. 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Để ý Dataview object DV1 được derived từ DS.alarm. Sau đó ta Sort các alarmstheo thứ tự ưu tiên, rồi trong số những alarm có cùng priority ta lại Sort chúngtheo datetime (ở đây data type của datetime chỉ là string).Ngoài ra để đếm con số các alarms thuộc mỗi priority ta có thể dùng Dataviewvới filter rồi xem property Count của nó như sau: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.ToStringEnd SubChắc bạn đã để ý thấy thay vì iterate qua mỗi record để đếm con số alarmsthuộc priority 1,2 hay 3, ta đã dùng ba Dataviews để filter ra alarms thuộc bapriorities khác nhau rồi lấy trị số Count của mỗi Dataview. Đây là lối lập trình dựavào những gì có sẵn càng nhiều càng tốt để tránh tạo ra bugs.Ngoài ra, để đếm con số hàng alarms được thật sự hiển thị bất cứ lúc nào tadùng BindingManagerBase object trong hai hàng code dưới đây:Dim bmb As BindingManagerBase = Me.BindingContext(DataGrid1.DataSource,DataGrid1.DataMember)NumDisplayed.Text = Displayed: & bmb.Count.ToStringTa đặt thêm ba buttons để filter alarms với code sau đây:Private Sub Btn1and2_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) _Handles Btn1and2.Click DV1.RowFilter = priority < 3 Dim bmb As BindingManagerBase =Me.BindingContext(DataGrid1.DataSource, DataGrid1.DataMember) NumDisplayed.Text = Displayed: & bmb.Count.ToStringEnd SubPrivate Sub Btn1Only_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) _Handles Btn1Only.Click DV1.RowFilter = priority = 1 Dim bmb As BindingManagerBase =Me.BindingContext(DataGrid1.DataSource, DataGrid1.DataMember) NumDisplayed.Text = Displayed: & bmb.Count.ToStringEnd SubPrivate Sub BtnAllAlarms_Click(ByVal sender As System.Object, ByVal eAs System.EventArgs) _Handles BtnAllAlarms.Click DV1.RowFilter = Dim bmb As BindingManagerBase =Me.BindingContext(DataGrid1.DataSource, DataGrid1.DataMember) NumDisplayed.Text = Displayed: & bmb.Count.ToStringEnd SubBạ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ácalarms được filtered như thế nào.Làm việc với một Row trong DataGridKhi một alarm mới được báo cáo và hiển thị, hệ thống điều khiển real-timethường hay phát ra những tiếng Beep nho nhỏ để nhắc Operator xử lý sự cố tạora alarm. Việc đầu tiên Operator sẽ làm là Acknowledge (xác nhận là tôi biếtrồi, khổ ...

Tài liệu được xem nhiều: