我在vb.Net 2008中使用组合框创建了一个datagridview
控件。在下面的代码中,我检索多行数据并配置我的datagrid
以正确显示数据。对于返回的所有行,我可以在datagrid
中填充每行的组合框,没有任何问题。
当我在检索特定记录的数据时尝试自动选择值时,所有组合框都会错误地填充相同的值。
在下面的代码中,“填充网格组合”代码表示我如何将值加载到组合框中。 “填充特定记录”代码是我认为我遇到问题的地方。为什么所有记录的组合框都返回相同的值?我知道我的循环是正确的,因为非组合框已正确填充。
'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 < 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 & "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 & "WHERE Audit_Outcome.Referral_Id = " & 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
非常感谢任何建议!
答案 0 :(得分:0)
我弄明白了这种情况。我改变了将值读入组合框的方式。我使用我熟悉的(记录集)切换到datareader。更轻松,更少的代码。
oConn.Open()
oCmdSelect.CommandText = "SELECT Referral_Reason_Id, Referral_Reason FROM Referral_Reason WHERE DELETED = FALSE ORDER BY SortOrder"
oCmdSelect.Connection = oConn
oReader = oCmdSelect.ExecuteReader
While oReader.Read
dgPPVReferralReason.ValueMember = oReader.Item(0)
dgPPVReferralReason.DisplayMember = oReader.Item(1).ToString
dgPPVReferralReason.Items.Add(oReader.Item(1).ToString)
End While
oConn.Close()
If oConn.State Then oConn.Close()