当我在另一行中选择值时,数据将从datagridviewcombobox中消失

时间:2019-04-26 14:16:19

标签: vb.net winforms datagridview

我在datagridview行中使用了多个datagridviewcombobox。 其中包括班次,职位以及每天7个名字。 它们是从这样的数据库中的字段填充的:

Private Sub Populate_DGV_ComboBoxes()
    Dim ShiftCmd As SqlCommand
    Dim ShiftSQL As String
    Dim ShiftConn As New SqlConnection()
    If GlobalVariables.logProd = 1 Then
        GlobalVariables.strConnection = "CCAPProdConnectionString"
    Else
        GlobalVariables.strConnection = "CCAPTestConnectionString"
    End If
    ShiftConn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings(GlobalVariables.strConnection).ConnectionString
    Try
        ShiftConn.Open()
        Dim i As Integer = 0
        For Each dr As DataGridViewRow In dgvCreateSchedule.Rows
            Dim ShiftName As New DataGridViewComboBoxCell
            With ShiftName
                Dim Shiftadapter As New SqlDataAdapter
                Dim ShiftDT As New DataTable
                'Dim val As String = dr.Cells("Shift Name").Value.ToString
                ShiftSQL = "select ShiftName from shifts where RecordActive = 1 order by ShiftName"
                ShiftCmd = New SqlCommand(ShiftSQL, ShiftConn)
                Using ShiftCmd
                    Shiftadapter.SelectCommand = ShiftCmd
                    Shiftadapter.Fill(ShiftDT)
                End Using
                .DataSource = ShiftDT
                .ValueMember = "ShiftName"
                .DisplayMember = "ShiftName"
                dgvCreateSchedule.Rows(i).Cells("ShiftName") = ShiftName
                i = i + 1
            End With
        Next
        ShiftConn.Close()
    Catch ex As Exception
        'MessageBox.Show(ex.Message)
    End Try

我选择了所有想要的值,然后转到下一个新行。 当我从新行的组合框中选择一个值时,所有值将从上一行中消失。 为什么会发生这种情况,我该如何阻止呢?

0 个答案:

没有答案