title: Loop through databases get the value and put it in a specific place in datagridview
body: <p>I have a <code>DataGridView</code> with 2 columns. The first column is populated with folder paths and the second column is empty. Inside each folder path is a single database named DB1. I would like to extract 1 value (VALUE) from each database and then put that value next to corresponding database path, in the second column. This is the query I am using</p>
  
  <pre><code>Select CODE, VALUE from DB1 where CODE = 2419
  </code></pre>
  
  <p>I know how to populate a <code>DataGridView</code> and how to extract 1 value from the database, but with this I don't even know where to begin.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I've managed to create working loop but don't know how to add those values to corresponding places in datagridview.</p>
  
  <pre><code> For Each row As DataGridViewRow In DataGridView1.Rows
          Dim sendtroopid As String
          sendtroopid = row.Cells("CODE").Value
          On Error Resume Next
          Dim FilePath As String = sendtroopid 'DATABASE PATH
          Using con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &amp; FilePath &amp; _
          " ;Extended Properties=dBASE IV")
              con.Open()
              Using cmd As New OleDbCommand("SELECT CODE, VALUE FROM DB1 WHERE CODE = @CODE", con)
                  cmd.Parameters.AddWithValue("@CODE", "2419")
                  Using reader As OleDbDataReader = cmd.ExecuteReader()
                      While (reader.Read())
                          MsgBox(reader("VALUE"))
                      End While
                  End Using
              End Using
          End Using
      Next
  </code></pre>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>With code above I get all values in msgbox. Only thing left is to insert another loop to put all those values (starting with row 0) to datagridview.
  If I replace msgbox with </p>
  
  <pre><code>For i As Integer = 0 To DataGridView1.Rows.Count - 1
  DataGridView1.Rows(i).Cells(1).Value = (reader("VALUE"))
  Next
  </code></pre>
  
  <p>then all rows are populated with only last value (value from last database).</p>
  
  <p><strong>EDIT 3</strong>
  I've changed </p>
  
  <pre><code>value = reader.Read() 
  </code></pre>
  
  <p>with    </p>
  
  <pre><code>While (reader.Read())
  value = reader("VALUE")
  End While
  </code></pre>