如何使我的datagridview更新更快 [英] How do I make my datagridview update faster
问题描述
大家好
我有一个程序可以使用通过TCP接收的新数据定期更新一些数据网格视图。我遇到的问题是屏幕刷新很慢。贝娄是我的代码的剥离版本。每次循环StartButton_Click中的循环时,此示例需要1.1s来更新屏幕。如何在不减少显示的数据量的情况下加快速度?
Hi All
I have a program that regularly updates a few data grid views with new data that is received via TCP. The problem I am having is that the screen refresh is quite slow. Bellow is a stripped back version of my code. This example takes 1.1s to update the screen each time the loop in StartButton_Click is iterated. How can i make this faster without reduicng the amount of data that is shown?
Public Class Form1
Private Sub Load_From(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Height = 700
Me.Width = 1000
DataGridView1.Location = New Point(10, 10)
DataGridView1.Width = Me.Width - 10
DataGridView1.Height = Me.Height - 10
DataGridView1.Rows.Clear()
DataGridView1.Columns.Clear()
'Set backcolour
DataGridView1.BackColor = Color.Black
DataGridView1.BackgroundColor = Color.Black
DataGridView1.DefaultCellStyle.BackColor = Color.Black
DataGridView1.DefaultCellStyle.SelectionBackColor = Color.Black
'Set forecolour
DataGridView1.ForeColor = Color.White
DataGridView1.DefaultCellStyle.ForeColor = Color.White
DataGridView1.DefaultCellStyle.SelectionForeColor = Color.White
For c As Integer = 0 To 20
DataGridView1.Columns.Add(New DataGridViewTextBoxColumn)
If DataGridView1.RowCount = 0 Then
DataGridView1.Rows.Add()
' ''LeaderBoardTable.Rows(0).Frozen = True
End If
DataGridView1.Columns(c).AutoSizeMode = DataGridViewAutoSizeColumnMode.None '0%
DataGridView1.Columns(c).Name = "col" & c
DataGridView1.Rows(0).Cells(c).Value = "col" & c
DataGridView1.Columns(c).Width = 40
'Header
DataGridView1.Rows(0).Cells(c).Style.Alignment = DataGridViewContentAlignment.MiddleCenter
DataGridView1.Rows(0).Cells(c).Style.WrapMode = DataGridViewTriState.True
DataGridView1.Rows(0).Cells(c).Style.Font = New Font("Verdana", 8, FontStyle.Bold)
'Data
DataGridView1.Columns(c).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
DataGridView1.Columns(c).DefaultCellStyle.WrapMode = DataGridViewTriState.False
DataGridView1.Columns(c).DefaultCellStyle.Font = New Font("Verdana", 8, FontStyle.Regular)
Next
DataGridView1.Rows.Add(25)
End Sub
Private Sub StartButton_Click(sender As Object, e As EventArgs) Handles StartButton.Click
Dim stpw As New Stopwatch
stpw.Reset()
stpw.Start()
For i As Integer = 0 To 10
Dim rand As New Random
Dim randnumber As Double = rand.Next(5, 15) / 10
UpdateDataGridView(randnumber)
DataGridView1.Update()
Me.Text = i & "/100"
Next
stpw.Stop()
MsgBox(stpw.Elapsed.TotalMilliseconds)
End Sub
Private Sub UpdateDataGridView(ByVal offset As Double)
For r As Integer = 1 To DataGridView1.RowCount - 1 'loop through rows
'Reset row
For c As Integer = 0 To DataGridView1.ColumnCount - 1 '10%
'Set Cell to default colour
DataGridView1(c, r).Style.BackColor = Color.Black '4%
DataGridView1(c, r).Style.ForeColor = Color.White '1%
DataGridView1(c, r).Style.Font = New Font("Verdana", 8, FontStyle.Regular) '0%
DataGridView1(c, r).Value = "" '0%
Next
'Set New Data
For c As Integer = 0 To DataGridView1.ColumnCount - 1 '89%
Dim colname As String = DataGridView1.Columns(c).Name '0%
DataGridView1.Rows(r).Cells(colname).Value = (r / c) * offset 'Round Off 0%
If DataGridView1.Rows(r).Cells(colname).Value > 1 Then '5%
DataGridView1.Rows(r).Cells(colname).Style.BackColor = Color.Red
End If
Next
Next
End Sub
End Class
我尝试了什么:
我添加了一个秒表来尝试锻炼什么行代码导致了最大的问题。这些测试的结果是每行代码后的注释中的%值。 %值是当该行代码被注释掉时的增益。
从测试中看,主要问题似乎是使用新数字更新datagridview单元格或颜色。所有其他代码行都没有什么区别。
我不知道如何让这个更快,因为我的程序依赖于定期更新的值和颜色。 datagridview不是此应用程序的正确对象吗?我应该使用其他东西吗?有没有办法让datagridview更快地更新?
What I have tried:
I added a stopwatch to try and work out what lines of code were causing the biggest issue. The results of these test are the % values in comments after each line of code. The % value is the gain when that line of code was commented out.
From the tests, it seemed that the main issue was updating the datagridview cells with a new number or colour. All other lines of code made very little difference.
I'm not sure how to make this faster as my program relies on the values and colours being updated regularly. Is a datagridview not the right object for this application? Should i be using something else? Is there a way to get a datagridview to update faster?
推荐答案
1)使用数据源而不是直接更新网格
或
2)使用一系列文本框/标签(或其他一些轻量级)
或
3)将所有列(连续)压缩为单个状态行/行/文本;明显的元素都不需要列(和标题)。
1) Use a "data source" instead of updating the grid directly
or
2) Use an array of text boxes / labels (or some other "light weights")
or
3) Condense all "columns" (in a row) to a single "status row / line / text"; obvious elements do not each need a "column" (and header).
这篇关于如何使我的datagridview更新更快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!