title: Reading every row in GridView on a Button Press
body: <p>I have a DataGrid which is created dynamically each time a user selects and item in another datagrid. I am missing one final step which has given me a 8 hour long headache. I need to read every row when a Button is pressed. Basically User press BUTTON and the program should read a value of each row (productID or Name[unique in this case]), assign a "1" value to it, insert into the database the information, and repeat the process with each row. I got until  here:</p>
  
  <pre><code>Protected Sub btnProcess_Click(sender As Object, e As EventArgs) Handles btnProcesss.Click
      For Each GridViewRow In GridView2.Rows
  
          'EmptySpace
      Next
  End Sub
  </code></pre>
  
  <p>And that's it. I know I have the loop correctly (right?) but I don't know what to add since I can't use this code for example:</p>
  
  <pre><code>Dim findrow = e.Row.Cell(cellIndex).FindControl("ControlID")
  </code></pre>
  
  <p>Since it'd give me an error in e.Row. Any help on how to proceed would be appreciated :).</p>
  
  <p>EDIT: Code for generating the columns under SelectedIndexChanged...</p>
  
  <pre><code>  If GridView2.Rows.Count = 0 Then
              '  dt = New DataTable()
              dt.Columns.Add(New DataColumn("Name", GetType(System.String)))
              dt.Columns.Add(New DataColumn("Price", GetType(System.String)))
          Else
              dt = DirectCast(Session("DataTable"), DataTable)
          End If
  </code></pre>
  
  <p>Added the whole code...</p>
  
  <pre><code>Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged
  
      .
      Dim row = GridView1.SelectedRow
      If Not row Is Nothing AndAlso IsNumeric(row.Cells(3).Text) Then
          Dim Price As Double = CDbl(row.Cells(3).Text)
          Dim Name As String = CStr(row.Cells(2).Text)
  
  
  
          If GridView2.Rows.Count = 0 Then
              '  dt = New DataTable()
              dt.Columns.Add(New DataColumn("Name", GetType(System.String)))
              dt.Columns.Add(New DataColumn("Price", GetType(System.String)))
          Else
              dt = DirectCast(Session("DataTable"), DataTable)
          End If
          Session("DataTable") = dt
  
          Dim dr1 As DataRow = dt.NewRow()
          dr1(0) = Name.ToString
          dr1(1) = CDbl(Price.ToString)
          dt.Rows.Add(dr1)
          GridView2.DataSource = dt
          GridView2.DataBind()
  
  
  
  
      End If
  </code></pre>
  
  <p>End Sub</p>
  
  <p>Updated Button Handler</p>
  
  <pre><code>Protected Sub btnProcess_Click(sender As Object, e As EventArgs) Handles btnProcess.Click
  
      For Each row As GridViewRow In GridView2.Rows
  
          Dim str As String = TryCast(row.FindControl("Name"), Label).Text
     'For testing purposes a MsgBox
          MsgBox(str)
  
      Next
  </code></pre>