title: VB.Net Datagridview to Datatable
body: <p>I wanted to create a datatable based from a 5 column datatable. Also, I would like to remove the last column (it is an image column).
  <br><br></p>
  
  <p>Basically, what I wanted to have is ( a pseudocode )
  <br></p>
  
  <pre><code>datatable = datagridview.datasource
  </code></pre>
  
  <p><br>
  I have tried this, but failed:
  <br></p>
  
  <pre><code>Dim dt As New DataTable
  dt = TryCast(dgvCarAccidentInjury.DataSource, DataTable)
  </code></pre>
  
  <p><br>
  And this,
  <br></p>
  
  <pre><code>Dim dt As New DataTable(dgvCarAccidentInjury.DataSource)
  </code></pre>
  
  <p><br>
  Again I failed.
  I saw this on the c# column but I don't know how to convert it to vb.net, maybe this is the solution but I don't know the syntax in VB.<br></p>
  
  <pre><code>DataTable data = (DataTable)(dgvMyMembers.DataSource);
  </code></pre>
  
  <p>I can do it by manually looping but is there an easy way like how it is on C#?</p>
  
  <p><br></p>
  
  <p><strong>EDIT</strong>
  I have set my datagridview's data manually by doing this line of code,</p>
  
  <pre><code>dgvCarAccidentInjury.Rows.Add(New String() {"test1", "test2", "test3", "test4"})
  </code></pre>
  
  <p>Also, there is a 5th column (an image column), actually it is empty. Sorry I have missed this important point out.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>the solution for this problem is this, by manually looping. </p>
  
  <pre><code>    Dim dt As New DataTable
      Dim r As DataRow
  
      dt.Columns.Add("a", Type.GetType("System.String"))
      dt.Columns.Add("b", Type.GetType("System.String"))
      dt.Columns.Add("c", Type.GetType("System.String"))
      dt.Columns.Add("d", Type.GetType("System.String"))
  
      For i = 0 To dgvCarAccidentInjury.Rows.Count - 1
          r = dt.NewRow
          r("a") = dgvCarAccidentInjury.Item(0, i).Value.ToString
          r("b") = dgvCarAccidentInjury.Item(1, i).Value.ToString
          r("c") = dgvCarAccidentInjury.Item(2, i).Value.ToString
          r("d") = dgvCarAccidentInjury.Item(3, i).Value.ToString
          dt.Rows.Add(r)
      Next
  </code></pre>
  
  <p>Other solutions:</p>
  
  <pre><code>Dim dt As New DataTable
  dt = TryCast(yourdatagridview.DataSource, DataTable)
  </code></pre>