Results 1 to 3 of 3

Thread: [RESOLVED] How to validate data on datagridview to prevent duplicate

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    75

    Resolved [RESOLVED] How to validate data on datagridview to prevent duplicate

    for example i have "NID" as textbox and datagrid with @NID field.
    i want save NID with Value "N001" to @NID on datagrid.
    how to prevent duplicate when i save NID with value "N001" to datagrid.

    I trying like this but error :
    Code:
    private sub save()  
    dim dgv as datagridview1  
    if dgv.CurrentRow.Cells(0).value = NID.text then  
    msgbox("Data duplicate")  
    else  
    dgv.rows.insert(.NewRowIndex, NID.text)  
    end if  
    end sub
    help me
    thanks

  2. #2
    Hyperactive Member AceDuk's Avatar
    Join Date
    Jan 2016
    Location
    Macedonia
    Posts
    465

    Re: How to validate data on datagridview to prevent duplicate

    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim Exists As Boolean = False
    3.         For Each item As DataGridViewRow In DataGridView1.Rows
    4.             If item.Cells(0).Value = NID.Text Then
    5.                 Exists = True
    6.             End If
    7.         Next
    8.         If DataGridView1.Rows.Count > 0 AndAlso Exists = True Then
    9.             MsgBox("Data duplicate")
    10.             Exit Sub
    11.         Else
    12.             DataGridView1.Rows.Insert(DataGridView1.NewRowIndex, NID.Text)
    13.         End If
    14.     End Sub
    Last edited by AceDuk; Jul 8th, 2017 at 07:23 AM. Reason: Improved
    Please Mark your Thread "Resolved", if the problem is solved & Rate those who have helped you

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    75

    Re: How to validate data on datagridview to prevent duplicate

    Problem solved ...
    I used this code -->

    Code:
     For i As Integer = 0 To dataspdibeli.Rows.Count - 1
                If Not IsDBNull(dataspdibeli.Rows(i).Cells("NID_SP").Value) AndAlso dataspdibeli.Rows(i).Cells("NID_SP").Value = spnid.Text Then
    thanks <-_->
    #junior_programmer

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width