搬家第二天-34.Wincc V7.3 MSHFGrid 冻结窗格

我们在使用Excel的时候,有时表格数据行太多,页面滚到后面的时候,往往忘记了某一列数据具体是什么参数,在Excel中可以使用窗格冻结来解决这个问题,MSHFGrid控件也有这个功能,下面就介绍如何在Wincc V7.3页面使用MSHFGrid控件查询数据表同时冻结窗格。

一 准备工作

假设SQL Server中有这样一张表

Wincc页面有一个MSHFGrid控件和一个按钮,最终在MSHFGrid显示的表有表头数据,是这个样子的:

我们希望竖直滚动条到下面时,表头不动作,那么按钮鼠标点击事件脚本如下:

Sub OnClick(ByVal Item)           
Dim conn
Dim ssql
Dim ors
Dim ocom
Dim scon
Dim MSHFGrid
Dim ADODC
Dim PCName
Dim i,j,InsertRowCount,colcount
PCName=HMIRuntime.Tags("@LocalMachineName").Read
scon="Provider = SQLOLEDB.1;Integrated Security=SSPI;Persist SecurityInfo=False;Initial Catalog =MyDB;Data Source = " &PCName & "\WINCC"
ssql="select * from charttable"
Set conn=CreateObject("ADODB.Connection")
conn.ConnectionString=scon
conn.Cursorlocation=3
conn.open
Set ors=CreateObject("ADODB.RecordSet")
Set ocom=CreateObject("ADODB.Command")
ocom.commandtype=1
Set ocom.ActiveConnection=conn
ocom.CommandText=ssql
Set ors=ocom.Execute
Set MSHFGrid=ScreenItems("MSHFGrid")
Set MSHFGrid.DataSource=ors
MSHFGrid.Refresh
MSHFGrid.colwidth(1)=2500
Set ors=Nothing
conn.close
Set conn=Nothing
'以下代码添加表头
InsertRowCount=3  '插入两行
For i=1 To InsertRowCount
   MSHFGrid.AddItem "",1
Next
'以下代码冻结第三行,冻结窗格要在“写表头之前做”
MSHFGrid.FixedRows =4  '冻结前四行
'MSHFGrid.FixedCols =2  '冻结前两列
'写“表头”
For i=1 To MSHFGrid.Cols-1
 MSHFGrid.TextMatrix(3,i)=MSHFGrid.TextMatrix(0,i)
Next
For i=1 To MSHFGrid.Cols-1
  MSHFGrid.TextMatrix(0,i)=""
Next
colcount=MSHFGrid.Cols
MSHFGrid.MergeCells= 1 '合并方式为自由合并
For j=1 To colcount-1
  MSHFGrid.TextMatrix(1,j)="***装置工艺参数表"
  MSHFGrid.TextMatrix(2,j)="查询时间:" & Now
Next
MSHFGrid.Mergerow(1)=True  '第一行合并
MSHFGrid.Mergerow(2)=True  '第二行合并
MSHFGrid.AllowUserResizing=3 '行高列宽皆可调整
MSHFGrid.Row=1
MSHFGrid.Col=1
'单元格对齐方式 CellAlignment
'0 左,顶部
'1 左,居中
'2 左,底部
'3 居中,顶部
'4 居中,居中
'5 居中,底部
'6 右,顶部
'7 右,居中
'8 右,底部
'9 默认方式:也即字符串左,居中;数值右,居中
MSHFGrid.CellAlignment=4  '水平居中,垂直居中
MSHFGrid.Row=2
MSHFGrid.Col=1
MSHFGrid.CellAlignment=7
'设置表头两行行高
MSHFGrid.Row=1
MSHFGrid.CellFontSize =14
MSHFGrid.Row=2
MSHFGrid.CellFontSize =14
MSHFGrid.RowHeight(1)=600
MSHFGrid.RowHeight(2)=600'以下代码添加表头
InsertRowCount=3  '插入两行
For i=1 To InsertRowCount
   MSHFGrid.AddItem "",1
Next
'写“表头”
For i=1 To MSHFGrid.Cols-1
 MSHFGrid.TextMatrix(3,i)=MSHFGrid.TextMatrix(0,i)
Next
For i=1 To MSHFGrid.Cols-1
  MSHFGrid.TextMatrix(0,i)=""
Next
colcount=MSHFGrid.Cols
MSHFGrid.MergeCells= 1 '合并方式为自由合并
For j=1 To colcount-1
  MSHFGrid.TextMatrix(1,j)="***装置工艺参数表"
  MSHFGrid.TextMatrix(2,j)="查询时间:" & Now
Next
MSHFGrid.Mergerow(1)=True  '第一行合并
MSHFGrid.Mergerow(2)=True  '第二行合并
MSHFGrid.AllowUserResizing=3 '行高列宽皆可调整
MSHFGrid.Row=1
MSHFGrid.Col=1
'单元格对齐方式 CellAlignment
'0 左,顶部
'1 左,居中
'2 左,底部
'3 居中,顶部
'4 居中,居中
'5 居中,底部
'6 右,顶部
'7 右,居中
'8 右,底部
'9 默认方式:也即字符串左,居中;数值右,居中
MSHFGrid.CellAlignment=4  '水平居中,垂直居中
MSHFGrid.Row=2
MSHFGrid.Col=1
MSHFGrid.CellAlignment=7
'设置表头两行行高
MSHFGrid.Row=1
MSHFGrid.CellFontSize =14
MSHFGrid.Row=2
MSHFGrid.CellFontSize =14
MSHFGrid.RowHeight(1)=600
MSHFGrid.RowHeight(2)=600

End Sub

保存后运行,就可以实现想要的效果了。

posted @ 2021-01-30 20:34  来自金沙江的小鱼  阅读(629)  评论(0编辑  收藏  举报