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()
DataGridView1.BackColor = Color.Black
DataGridView1.BackgroundColor = Color.Black
DataGridView1.DefaultCellStyle.BackColor = Color.Black
DataGridView1.DefaultCellStyle.SelectionBackColor = Color.Black
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()
End If
DataGridView1.Columns(c).AutoSizeMode = DataGridViewAutoSizeColumnMode.None
DataGridView1.Columns(c).Name = "col" & c
DataGridView1.Rows(0).Cells(c).Value = "col" & c
DataGridView1.Columns(c).Width = 40
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)
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
For c As Integer = 0 To DataGridView1.ColumnCount - 1
DataGridView1(c, r).Style.BackColor = Color.Black
DataGridView1(c, r).Style.ForeColor = Color.White
DataGridView1(c, r).Style.Font = New Font("Verdana", 8, FontStyle.Regular)
DataGridView1(c, r).Value = ""
Next
For c As Integer = 0 To DataGridView1.ColumnCount - 1
Dim colname As String = DataGridView1.Columns(c).Name
DataGridView1.Rows(r).Cells(colname).Value = (r / c) * offset
If DataGridView1.Rows(r).Cells(colname).Value > 1 Then
DataGridView1.Rows(r).Cells(colname).Style.BackColor = Color.Red
End If
Next
Next
End Sub
End Class
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?