Wincc Script Example 3 – Sorting Records in the MSHFGrid Control by Fields
Thread Content
Sometimes we also need to enable operators to sort by fields after clicking on the MSHFGrid control; this article explains how to achieve this functionality. 1. Preparation: Create a new internal variable of 32-bit floating-point type named Sort in Wincc, to indicate whether the sorting should be in ascending or descending order. Insert a static text element on the WinCC page; rename it to “txt”. Set its Property – Effects – Global Color to “No”. For the Property – Color – Background Color, set the transparency to 100 and the border thickness to 0.For the sorting script, enter the following code in the MSHFGrid control’s event – Object Events – MouseUp:
Dim MSHFGrid, txt, sort
Set MSHFGrid = ScreenItems(“MSHFGrid”)
Set sort = HMIRuntime.Tags(“Sort”)
Set txt = ScreenItems(“txt”)
If MSHFGrid.TextMatrix(0, item.MouseCol) = “Date and Time” Then
If sort.Read = 2 Then
MSHFGrid.Sort = 1 ‘Sort in ascending numerical order
sort.Write 1
txt.Text = “Currently sorted in ascending order based on the “ & MSHFGrid.TextMatrix(0, item.MouseCol) & “ field.”
Else
MSHFGrid.Sort = 2 ‘Sort in descending numerical order
sort.Write 2
txt.Text = “Currently sorted in descending order based on the “ & MSHFGrid.TextMatrix(0, item.MouseCol) & “ field.”
End If
Else If sort.Read = 2 Then
MSHFGrid.Sort = 5 ‘Sort in ascending alphabetical order
sort.Write 1
txt.Text = “Currently sorted in ascending order based on the “ & MSHFGrid.TextMatrix(0, item.MouseCol) & “ field.”
Else
MSHFGrid.Sort = 6 ‘Sort in descending alphabetical order
sort.Write 2
txt.Text = “Currently sorted in descending order based on the “ & MSHFGrid.TextMatrix(0, item.MouseCol) & “ field.”
End If
End Sub
Save and run the script to see the result. This script is based on the project from the previous blog post. For information on how to connect MSHFGrid to a database, please refer to other posts that provide recommendations for WinCC scripts.