title: BindingSource with DataTable in VB
body: <p><strong>Preamble:</strong>
  Having not worked with WinForms for a long while and also never before in VB I am trying to add a <code>DataGridView</code> to an application written in VB which will display a grid of data from a <code>DataTable</code>.</p>
  
  <p>I followed the documentation <a href="http://msdn.microsoft.com/en-us/library/system.data.datarow.aspx" rel="nofollow">here</a>, <a href="http://msdn.microsoft.com/en-us/library/system.data.datacolumn.aspx" rel="nofollow">here</a> and <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.aspx" rel="nofollow">here</a> and in a simple test example I have the code</p>
  
  <pre><code>Public Class Form1
  
      Private count As Integer
  
      Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
          count = count + 1
  
          Dim table As DataTable = BindingSource2.DataSource
  
          Dim row As DataRow
          row = table.NewRow()
          row("Col1") = "foo" + count.ToString()
          row("Col2") = "bar" + count.ToString()
  
          table.Rows.Add(row) 'throws System.InvalidOperationException here
  
      End Sub
  
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
          'BindingSource2.DataSource = New DataTable()
          'Dim table As DataTable = BindingSource2.DataSource
          Dim table As New DataTable
  
          Dim column1 As DataColumn = New DataColumn()
          column1.ColumnName = "Col1"
          column1.Caption = column1.ColumnName
          column1.DataType = System.Type.GetType("System.String")
          table.Columns.Add(column1)
  
          Dim column2 As DataColumn = New DataColumn()
          column2.ColumnName = "Col2"
          column2.Caption = column2.ColumnName
          column2.DataType = System.Type.GetType("System.String")
          table.Columns.Add(column2)
  
          'Dim keys(0) As DataColumn
          'keys(0) = column1
          'table.PrimaryKey = keys
  
          ' first row
          Dim row As DataRow = table.NewRow()
          row("Col1") = "beep"
          row("Col2") = "boop"
  
          table.Rows.Add(row)
  
          BindingSource2.DataSource = table
      End Sub
  End Class
  </code></pre>
  
  <p>The code gets through <code>Form1_Load</code> okay however the entries added there are not shown in the <code>DataGridView</code>. Then when <code>Timer1_Tick</code> is called it throws a <code>System.InvalidOperationException</code> exception at the line indicated above. I cannot see what I am doing wrong based on the examples given in the docs.</p>
  
  <p><strong>Question:</strong> Can anybody help please, with (a) why does the <code>DataGridView</code> not reflect the added data at the end of <code>Form1_Load</code> and (b) why is adding a row causing an exception?</p>
  
  <p><strong>P.s.</strong> I've checked the debug and at the <code>table.Rows.Add(row)</code> <code>table</code> contains the correct information as does <code>row</code>.</p>
  
  <p><strong>Edit:</strong> The BindingSource was added and connected to the DataGridView using the designer so the code for it is shown instead in From1.Designer.vb which I haven't shown here.</p>