Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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?


VB
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


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?
Posted
Updated 10-Aug-18 11:03am
Comments
Richard MacCutchan 10-Aug-18 3:48am    
Most likely because you are updating the properties of your grid every time. You should set the formats (colours, font styles etc) when the grid is created. The update should just add or modify the content.
Jasonwc 12-Aug-18 23:01pm    
I can set the fonts when the grid is created but the cell back colour is dependent on the data so I need to update it as the data updates.

1 solution

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).
 
Share this answer
 
Comments
Jasonwc 12-Aug-18 19:36pm    
Thanks Gerry I'll give that a try
Jasonwc 14-Aug-18 16:35pm    
I tried using data tables but it made no difference. I'll try using an array of labels instead and see if that helps.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900