title: Display(Graphs & DataGridViews) Slow to Update
body: <p>I have a program (made with .net using visualstudio 2012 desktop) that needs to update a few datagridviews and graphs with new data every second. The issue i am having is that the form pauses for a long time (approx 1-2sec) while these datagridviews are being updated. There are 7 datagridviews and 2 graphs.</p>
  
  <p>The datagridviews are updated by looping through each row and then updating the cells individually. The display is then updated with a "datagridview.update()" for each datagridview.</p>
  
  <p>The graphs have new points added as required and then are also updated using graph.update().</p>
  
  <p>I have done some research and added a few things like making the column widths fixed. But running the script does not seem to be what is taking the time.</p>
  
  <p>I have run a profiler over the program which showed that most of the time (approx 90%) is spent in the OnPaint method which is called every time i call datagridview.update() or graph.update()</p>
  
  <p>Does anyone have any advice on how i can make this more effecient but still updating the form at the same rate. Most of the googling ablout On.paint is about drawing lines and points</p>
  
  <p>Updated 22/02/2017
  Here's my attempt at summarizing it for one of the datagridviews. Thanks in advance sorry for being a noob.</p>
  
  <p><pre><code></p>
  
  <code>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
  </code></pre>
  
  <p></code></p>