title: Group datatable entries by row value after having separated
body: <p><strong><em>Whats happening:</em></strong>  I have a table consisting of Name, ID, Comments.  Each ID is unique unless the user submits more than 1 entry.  At that point that user may then have 3 rows where the ID is the same to show these entries were all entered by 1 user and they belong together.</p>
  
  <p>Below I'll post what I had done originally to separate these into separate datagridview's with buttons below each one that will eventually be used to work with the datagrid entries above it.</p>
  
  <p>So this leads into 2 things I've trying to figure out and haven't had much luck through trial and error and Google researching.</p>
  
  <p>1.)  How can I make this put any entries in the datatable that have the same ID into the same datagridview rather than creating a separate datagrid for each one.</p>
  
  <p>2.)  Assuming the above can be done how would I, if possible, go about making the button generating below each one only correspond to the datagrid above it?  Can each datagrid and button although created programatically be assigned a unique ID that allows each one to correspond to the other?</p>
  
  <pre><code>    Dim dst As New DataSet
      For i As Integer = 0 To DT.Rows.Count - 1
          Dim intLastRow As Integer = i
          If intLastRow &gt; DT.Rows.Count - 1 Then intLastRow = DT.Rows.Count - 1
          Dim dtbNew As DataTable = DT.Clone
          dtbNew.TableName = DT.TableName
          For j As Integer = i To intLastRow
              dtbNew.ImportRow(DT.Rows(j))
          Next j
          dst.Tables.Add(dtbNew)
      Next i
  
      For Each table As DataTable In dst.Tables
          Dim DGV As New DataGridView
          Dim BTN As New Button
          BTN.Text = "Show Details"
          BTN.Width = 120
          DGV.DataSource = table
          DGV.Width = 800
          DGV.Height = 80
          FlowLayoutPanel1.Controls.Add(DGV)
          FlowLayoutPanel1.Controls.Add(BTN)
      Next
      FlowLayoutPanel1.AutoScrollPosition = New Point(FlowLayoutPanel1.Top)
  </code></pre>
  
  <p><strong>Errors</strong></p>
  
  <pre><code>Dim DT As New DataTable
      DT.Load(SQLMP)
  
      Dim groups As IEnumerable(Of IGrouping(Of Integer, DataRow)) = DT.Select.GroupBy(Function(row) row.Field(Of Integer)("ORDER_ID"))
  </code></pre>
  
  <p>An unhandled exception of type 'System.InvalidCastException' occurred in System.Data.DataSetExtensions.dll`</p>
  
  <p>Additional information: Specified cast is not valid</p>