我有一个程序(使用.net使用visualstudio 2012桌面制作),需要每秒更新一些数据网格图和新数据。我遇到的问题是,在更新这些数据网格时,表单会暂停很长时间(大约1-2秒)。有7个datagridviews和2个图表。
通过循环遍历每一行然后单独更新单元格来更新datagridviews。然后使用每个datagridview的“datagridview.update()”更新显示。
图表根据需要添加了新点,然后使用graph.update()更新。
我做了一些研究并添加了一些内容,比如修改列宽。但是运行脚本似乎并没有花费时间。
我在程序上运行了一个分析器,它显示大多数时间(大约90%)花在OnPaint方法上,每次调用datagridview.update()或graph.update()
有没有人对如何使这个更有效但仍然以相同的速率更新表格有任何建议。关于On.paint的大部分谷歌搜索是关于绘制线条和点
2017年2月22日更新 这是我尝试总结其中一个datagridviews。在此先感谢抱歉作为一个菜鸟。
Public Class Session
Public DataGroups(999) As DataGroup
End Class
Public Class DataGroup
Dim CurrentLineRow as integer
Dim Value1 as Double
Dim Value2 as Double
Dim Value3 as Double
Dim Value4 as Double
'etc...
End Class
Public Class Form1
Public CurrentSession As New Session
'The following data is updated as it is read from a text file elsewhere in the program
CurrentSession.DataGroups(Index1FromTextFile).CurrentLineRow = LineRowValueFromTextFile
CurrentSession.DataGroups(Index1FromTextFile).Value1 = Value1FromTextFile
CurrentSession.DataGroups(Index1FromTextFile).Value2 = Value2FromTextFile
etc....
CurrentSession.DataGroups(Index2FromTextFile).CurrentLineRow = LineRowValueFromTextFile2
CurrentSession.DataGroups(Index2FromTextFile).Value1 = Value1FromTextFile2
CurrentSession.DataGroups(Index2FromTextFile).Value2 = Value2FromTextFile2
etc.. etc..
Private Sub CellValueChanged()
For r As Integer = 1 To DataGridView1.RowCount - 1 'loop through rows
'Sets Default Colours and Styles. These need to be reset as they can get changed later in the sub
t For c As Integer = 0 To DataGridView1.ColumnCount - 1
'Set Cell to default colour
DataGridView1(c, r).Style.BackColor = Color.Black
DataGridView1(c, r).Style.ForeColor = Color.White
DataGridView1(c, r).Style.Font = New Font("Verdana", 12, FontStyle.Regular)
Next
'Get DataGroup that matches row
Dim RowDataGroup As DataGroup = Nothing
For i As Integer = 1 To CurrentSession.DataGroups.Length - 1
If CurrentSession.DataGroups(i) IsNot Nothing Then
If CurrentSession.DataGroups(i).CurrentLineRow = r Then
RowDataGroup = CurrentSession.DataGroups(i)
End If
End If
Next
If RowDataGroup IsNot Nothing Then 'Skip code if DataGroup could not be found
'Similar code to this is repeated for each column (total of 40 columns)
If RowDataGroup.Value 1 Then
DataGridView1.Rows(r).Cells("Column1").Value = Math.Round(owDataGroup.Value1, 2)
DataGridView1.Rows(r).Cells("Column1").Style.ForeColor = Color.Green
End If
End If
Next
DataGridView1.Update() 'Acording to the profiller this is what is taking the majority of the time
End Sub
End Class
Public Class Session
Public DataGroups(999) As DataGroup
End Class
Public Class DataGroup
Dim CurrentLineRow as integer
Dim Value1 as Double
Dim Value2 as Double
Dim Value3 as Double
Dim Value4 as Double
'etc...
End Class
Public Class Form1
Public CurrentSession As New Session
'The following data is updated as it is read from a text file elsewhere in the program
CurrentSession.DataGroups(Index1FromTextFile).CurrentLineRow = LineRowValueFromTextFile
CurrentSession.DataGroups(Index1FromTextFile).Value1 = Value1FromTextFile
CurrentSession.DataGroups(Index1FromTextFile).Value2 = Value2FromTextFile
etc....
CurrentSession.DataGroups(Index2FromTextFile).CurrentLineRow = LineRowValueFromTextFile2
CurrentSession.DataGroups(Index2FromTextFile).Value1 = Value1FromTextFile2
CurrentSession.DataGroups(Index2FromTextFile).Value2 = Value2FromTextFile2
etc.. etc..
Private Sub CellValueChanged()
For r As Integer = 1 To DataGridView1.RowCount - 1 'loop through rows
'Sets Default Colours and Styles. These need to be reset as they can get changed later in the sub
t For c As Integer = 0 To DataGridView1.ColumnCount - 1
'Set Cell to default colour
DataGridView1(c, r).Style.BackColor = Color.Black
DataGridView1(c, r).Style.ForeColor = Color.White
DataGridView1(c, r).Style.Font = New Font("Verdana", 12, FontStyle.Regular)
Next
'Get DataGroup that matches row
Dim RowDataGroup As DataGroup = Nothing
For i As Integer = 1 To CurrentSession.DataGroups.Length - 1
If CurrentSession.DataGroups(i) IsNot Nothing Then
If CurrentSession.DataGroups(i).CurrentLineRow = r Then
RowDataGroup = CurrentSession.DataGroups(i)
End If
End If
Next
If RowDataGroup IsNot Nothing Then 'Skip code if DataGroup could not be found
'Similar code to this is repeated for each column (total of 40 columns)
If RowDataGroup.Value 1 Then
DataGridView1.Rows(r).Cells("Column1").Value = Math.Round(owDataGroup.Value1, 2)
DataGridView1.Rows(r).Cells("Column1").Style.ForeColor = Color.Green
End If
End If
Next
DataGridView1.Update() 'Acording to the profiller this is what is taking the majority of the time
End Sub
End Class