title: How do I populate a VB.net datagridview combo box properly
body: <p>I created a <code>datagridview</code> control with a combo box in vb.Net 2008.  In the code below, I retrieve multiple rows of data and configure my <code>datagrid</code> to properly show the data.  For all the rows returned, I can populate each row's combo box in the <code>datagrid</code> with no problem.</p>
  
  <p>When I try to auto select value(s) when retrieving data for a specific record, all of the combo boxes are incorrectly populated with the same value.  </p>
  
  <p>In the code below, 'Populate the Grid Combo' code represents how I load the values into the combo box.  The 'Populate specific record' code is where I believe I'm having the issue. Why is the combo box for all records returned the same value?  I know my loop is correct because non-combo boxes are properly populated.</p>
  
  <pre class="lang-vb prettyprint-override"><code>'Populate the Grid Combo
              conCombo.Open(sConnectionString)
  
          'Populate the Referral Reason combo box
          sSQL = "SELECT Referral_Reason_Id, Referral_Reason FROM Referral_Reason WHERE DELETED = FALSE ORDER BY SortOrder"
  
          cmdCombo = New ADODB.Command
          cmdCombo.ActiveConnection = conCombo
          cmdCombo.CommandText = sSQL
          rsCombo = cmdCombo.Execute
  
          i = 0
          Do While i &lt; dgPPV.RowCount
              Do While Not rsCombo.EOF
                  Dim dgvcc As DataGridViewComboBoxCell
                  dgvcc = dgPPV.Rows(i).Cells(3)
                  dgvcc.Items.Add(New MyListItem(rsCombo.Fields(1).Value, rsCombo.Fields(0).Value))
                  rsCombo.MoveNext()
              Loop
              rsCombo.MoveFirst()
              i = i + 1
          Loop
  
          If rsCombo.State Then rsCombo.Close()
          If conCombo.State Then conCombo.Close()
          If cmdCombo.State Then cmdCombo.ActiveConnection = Nothing
  
  
  'Populate specific record
          Sub GetPPVGridData()
  
          Dim i As Integer
  
          CloseConnectionString()
          con.Open(sConnectionString)
  
          sSQL = "SELECT Policy.PolicyDescription, Referral_Reason.Referral_Reason, ERS_Outcome.Outcome_Description, Audit_Outcome.Comment "
          sSQL = sSQL &amp; "FROM ((Audit_Outcome INNER JOIN Referral_Reason ON Audit_Outcome.Referral_Reason_Id = Referral_Reason.Referral_Reason_Id) INNER JOIN Policy ON Audit_Outcome.Policy_Id = Policy.Policy_ID) INNER JOIN ERS_Outcome ON Audit_Outcome.Outcome_Id = ERS_Outcome.Outcome_Id "
          sSQL = sSQL &amp; "WHERE Audit_Outcome.Referral_Id = " &amp; lblReferralId.Text
  
          cmd = New ADODB.Command
          cmd.ActiveConnection = con
          cmd.CommandText = sSQL
          rs = cmd.Execute
  
          Do While Not rs.EOF
              For i = 0 To dgPPV.RowCount - 1
                  If dgPPV.Rows(i).Cells("dgPPVDescription").Value = rs.Fields(0).Value Then
                      dgPPV.Rows(i).Cells("dgCheckPPV").Value = True
                      dgPPV.Rows(i).Cells("dgPPVDescription").Value = rs.Fields(0).Value
                      dgPPV.Rows(i).Cells("dgPPVReferralReason").Value = rs.Fields(1).Value
                      dgPPV.Rows(i).Cells("dgPPVOutcome").Value = rs.Fields(2).Value
                      dgPPV.Rows(i).Cells("dgPPVComment").Value = rs.Fields(3).Value
                  End If
              Next
              rs.MoveNext()
          Loop
          CloseConnectionString()
  
      End Sub
  </code></pre>
  
  <p>Any advice is greatly appreciated!</p>