title: resizing and redrawing datagridview
body: <p>I uses datagridview for show query results from database which may have 0 to x number of rows.<br>
  So I made calculation to calculate size of underlaying form and my datagridview dependable on number of matched rows.<br>
  Underlying form is transparent and all of that looks like user control what appears and works just fine.  </p>
  
  <p>But here is one issue:<br>
  Every time datagrid have to grow, black square in that area is showed before datagrid is filled, what is not nice and surely unwanted. </p>
  
  <p>Can I do something usual to avoid this?<br>
  Did datagridview have some mechanism to freze it and show data when populating is finished?<br>
  Or what else to do?</p>
  
  <pre><code>   Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
  
      Dim tw As Integer = 0
      Dim n As Integer = 0
      Dim sqlText As String
      Dim reader As OdbcDataReader = Nothing
      Dim btCommand As OdbcCommand = Nothing
      Dim mCmd As OdbcCommand = Nothing
      Dim mCon As New Odbc.OdbcConnection
  
      DataGridView1.Rows.Clear()
      If Trim(TextBox1.Text).Length Then
          mCon.ConnectionString = "Dsn=" + dbDsn + _
                                  ";database=" + mydatabase + ";server=" + dbServer + ";port=" + dbPort + _
                                  ";uid=" + dbUser + ";pwd=" + dbPass
          Try
              mCon.Open()
              btCommand = New OdbcCommand("BEGIN TRANSACTION", mCon)
              sqlText = "SELECT dtbl_id, name... etc... FROM mytable WHERE name ILIKE '%" &amp; Trim(TextBox1.Text) &amp; "%' ORDER BY name LIMIT 128"
              mCmd = New OdbcCommand(sqlText, mCon)
              reader = mCmd.ExecuteReader()
              While (reader.Read())
  
                  Dim t_kol, t_ci As String
                  If reader.GetValue(4) - reader.GetValue(5) = 0 Then
                      t_kol = "- "
                  Else
                      t_kol = FormatNumber((reader.GetValue(4) - reader.GetValue(5)), 2)
                  End If
                  t_ci = FormatNumber(reader.GetValue(3))
  
                  With DataGridView1.Rows.Add(New String() {reader.GetValue(0).ToString(), _
                                                            reader.GetValue(1).ToString(), _
                                                            reader.GetValue(2).ToString(), _
                                                            t_kol, _
                                                            t_ci, _
                                                            reader.GetValue(6).ToString(), _
                                                            reader.GetValue(7).ToString})
                      n = n + 1
                  End With
              End While
              btCommand = New OdbcCommand("END TRANSACTION", mCon)
          Catch ex As Exception
              MsgBox(ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly)
          Finally
              reader.Close()
              reader.Dispose()
              btCommand.Dispose()
              mCmd.Dispose()
  
              If n &lt; 1 Then
                  DataGridView1.Height = 0
                  DataGridView1.Height = DataGridView1.Height + DataGridView1.Top
              End If
  
          End Try
          mCon.Close()
          mCon.Dispose()
      End If
  
      If n &lt; 1 Then
          DataGridView1.Height = 0
      Else
          DataGridView1.Height = DataGridView1.ColumnHeadersHeight + (n * DataGridView1.RowTemplate.Height) + 2
      End If
  
      Dim p As Point = Me.PointToScreen(DataGridView1.Location)
      If p.Y + DataGridView1.Height &gt;= My.Computer.Screen.WorkingArea.Height Then DataGridView1.Height = My.Computer.Screen.WorkingArea.Height - p.Y
  
      tw = totalwidth + (Math.Abs(CInt(DataGridView1.Controls(1).Visible)) * CInt(DataGridView1.Controls(1).Width))
      DataGridView1.Width = tw + 2
      With Me
          .Width = tw + 4
          .Height = DataGridView1.Height + DataGridView1.Top
      End With
  End Sub
  </code></pre>