title: How to get the value of DataGridView.SelectedRows().Cells() from different forms?
body: <p>I have 2 forms and within each of the 2 forms there is a <code>DataGridView</code>(<code>chatform</code> and <code>prodetail</code>). </p>
  
  <p>In the <code>chatform</code> I created a <code>DataGridView</code> which has a generated <code>Button</code> in each row. </p>
  
  <p>Each <code>Button</code> when clicked will load a <code>prodetail</code> form and when in the <code>prodetail</code> form I want to get the value of the <code>SelectedRow.Cell</code> from the <code>DataGridView</code> in the originating <code>chatform</code>.</p>
  
  <p>Code (<code>chatform</code>):</p>
  
  <pre><code>Public Sub loadtoDGV()
      Dim sqlq As String = "SELECT * FROM chattbl"
      Dim sqlcmd As New SqlCommand
      Dim sqladpt As New SqlDataAdapter
      Dim tbl As New DataTable
  
      With sqlcmd
          .CommandText = sqlq
          .Connection = conn
      End With
  
      With sqladpt
          .SelectCommand = sqlcmd
          .Fill(tbl)
      End With
  
      DataGridView1.Rows.Clear()
      For i = 0 To tbl.Rows.Count - 1
          With DataGridView1
              .Rows.Add(tbl.Rows(i)("Username"), tbl.Rows(i)("Title"), tbl.Rows(i)("ChatDateTime"))
          End With
      Next
      conn.Close()
  End Sub
  
  Private Sub ChatForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      loadtoDGV()
  End Sub
  </code></pre>
  
  <p>Code (<code>DataGridView1.CellContentClick</code>):</p>
  
  <pre><code>Private Sub grdData_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
      Dim colName As String = DataGridView1.Columns(e.ColumnIndex).Name
      If colName = "Detail" Then
          Prodetail.Show()
          MessageBox.Show(String.Format("You clicked the button in row {0} of the Detail column", e.RowIndex))
      End If
  End Sub
  </code></pre>
  
  <p>Code (<code>prodetail</code>):</p>
  
  <pre><code>Public Sub loadtoDGV2()
  
      Dim i As Integer
      i = ChatForm.DataGridView1.SelectedRows.Count
      MsgBox(i)
  
      Dim compareai As String = ChatForm.DataGridView1.SelectedRows(i).Cells(1).Value
      Dim sqlq As String = "SELECT * FROM Chattbl WHERE Title =" &amp; compareai &amp; ""
      Dim sqlcmd As New SqlCommand
      Dim sqladpt As New SqlDataAdapter
      Dim tbl As New DataTable
  
      With sqlcmd
          .CommandText = sqlq
          .Connection = conn
      End With
  
      With sqladpt
          .SelectCommand = sqlcmd
          .Fill(tbl)
      End With
  
      DataGridView1.Rows.Clear()
      For i = 0 To tbl.Rows.Count - 1
          With DataGridView1
              .Rows.Add(tbl.Rows(i)("Username"), tbl.Rows(i)("Title"), tbl.Rows(i)("ChatDateTime"), tbl.Rows(i)("ChatContent"))
          End With
      Next
      conn.Close()
  End Sub
  
  Private Sub Prodetail_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      loadtoDGV2()
  End Sub
  </code></pre>
  
  <p>What have I done wrong? </p>
  
  <p>I tried to use <code>MsgBox(i) i = SelectedRow(0)</code> assuming it would show the data for first row, but <code>DataGridView1</code> in <code>prodetail</code> does not load any data from the database. </p>
  
  <p>I did not observe any errors, I just don't have a solution.</p>