none
Searching a Database for certain value RRS feed

  • Question

  • I'm working on a VB.net project using an access database.  I have a datagridview to view the data.  When the user adds a new cell I need to check if the value the user is entering has already been entered.  Any advice on how to do this?
    Tuesday, February 19, 2019 7:54 PM

Answers

  • If the DataGridView.DataSource is set to a DataTable subscribe to DataColumnChanging event of the DataTable.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by nilebra27 Wednesday, February 27, 2019 7:06 PM
    Thursday, February 21, 2019 7:21 PM
    Moderator

All replies

  • Hello,

    Do you mean you want to determine when a new value is entered into an existing DataGridViewCell where the row exists already or when adding a new DataGridViewRow ?


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, February 19, 2019 11:33 PM
    Moderator
  • Hi,

    If you want to enter the value of the certain column is not repeated, such as the third column, you can use the following way:

      Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
            Dim t = 2
            If Me.DataGridView1.Rows.Count > 1 AndAlso e.RowIndex > 0 Then
                If e.ColumnIndex = t Then
                    For i As Integer = 0 To e.RowIndex - 1
                        If Me.DataGridView1.Rows(i).Cells(t).Value.ToString() = Me.DataGridView1.Rows(e.RowIndex).Cells(t).Value.ToString() Then
                            MessageBox.Show("The value is repeated", "Warning")
                            Exit For
                        End If
                    Next
                End If
            End If
        End Sub

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, February 20, 2019 1:57 AM
  • When row already exists
    Thursday, February 21, 2019 7:10 PM
  • If the DataGridView.DataSource is set to a DataTable subscribe to DataColumnChanging event of the DataTable.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by nilebra27 Wednesday, February 27, 2019 7:06 PM
    Thursday, February 21, 2019 7:21 PM
    Moderator