Datagridview - 防止在comboboxcell中重复

时间:2016-10-13 09:17:30

标签: vb.net datagridview

我在Datagridview的第一个单元格中有一个ComboboxCell。这个组合框有我的DB的Datasource。当我从它中选择项目时,一行中的所有其他单元格将由我的数据库中的记录填充。现在我想根据Comboboxcell选择的项目来防止重复的行条目。之后我想清除Comboboxcell,并保持代码运行。我设法做了几乎所有事情,但问题是在显示msgbox之后,代码停止工作 - 所以当我再次选择ComboboxCell Item时,即使在新行中也没有任何反应。这是我的全部代码:

 Private Sub My_DGV_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles My_DGV.CellValueChanged

        If My_DGV.Columns(e.ColumnIndex).Name = "Column1" Then

            'Prevent duplicates
            For i As Integer = 0 To My_DGV.RowCount - 2
                For j As Integer = i + 1 To My_DGV.RowCount - 2
                    If My_DGV.Rows(i).Cells(0).Value = My_DGV.Rows(j).Cells(0).Value Then
                        MsgBox("You allready selected this item. Duplicates are not allowed.", MsgBoxStyle.Information, "Warning")
                        My_DGV.Rows(j).Cells(0).Value = " "
                        Dim cbx As ComboBox = DGV_APO.EditingControl
                        cbx.SelectedIndex = -1
                        Exit Sub
                     End If

                 Next
             Next

            OracleconnOpen()

            Using cmd As New OracleCommand()

                Dim SQL As String = "Select NAMES,SURNAMES,STATE FROM My_Table"
                Dim Concat_SQL As String = " Where "
                SQL = String.Concat(SQL, Concat_SQL, " ID_NUMBER = :id")
                cmd.Parameters.Add(New OracleParameter("id", My_DGV.CurrentRow.Cells(0).Value))
                cmd.Connection = OracleconnOpen()
                cmd.CommandText = SQL
                cmd.CommandType = CommandType.Text

                Dim dr As OracleDataReader = cmd.ExecuteReader()
                Dim dt As New DataTable
                dt.Load(dr)

                My_DGV.CurrentRow.Cells(1).Value = dt.Rows(0)("NAMES").ToString()
                My_DGV.CurrentRow.Cells(2).Value = dt.Rows(0)("SURNAMES").ToString()
                My_DGV.CurrentRow.Cells(3).Value = dt.Rows(0)("STATE").ToString()

            End Using

            OracleconnClose()

        End If

 End Sub

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        'SELECT from my DB
        Dim SQL As String = "SELECT ID_NUMBER from My_Table"
        Dim dtb As New DataTable()

        Try

            OracleconnOpen() 'Open my connection

            Using dad As New OracleDataAdapter(SQL, OracleconnOpen)
                dad.Fill(dtb)
            End Using

            Column1.DisplayMember = "ID_NUMBER"
            Column1.DataSource = dtb

        Catch ex As Exception
            MessageBox.Show(ex.Message)

        Finally

            OracleconnClose() 'Close my connection
        End Try

 End Sub

 Private Sub My_DGV_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles My_DGV.CurrentCellDirtyStateChanged

        If My_DGV.IsCurrentCellDirty Then My_DGV.CommitEdit(DataGridViewDataErrorContexts.Commit)

 End Sub

 Private Sub My_DGV_DataError(sender As Object, e As DataGridViewDataErrorEventArgs) Handles DGV_APO.DataError

        e.ThrowException = False
 End Sub

那么,我做错了什么?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

您的循环中有一个Exit Sub调用,它将始终执行,因此当您更改此值时,将不会运行exit sub。如果要在找到重复后退出循环并继续运行代码,则应在If语句中完成。在比较第一行后,即使不匹配,此代码也应停止工作。