Click here to Skip to main content
15,888,968 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have "N" as textbox with value "001" and i have datagridview(dgv) with "@ka" field
I insert "001" to dgv --->

@ka
001
...
...

how to prevent duplicate when i try to insert 001 again to dgv..
please help me guys..
thanks

What I have tried:

if N.textbox=dgv.currentrow.cells(0).value then
msgbox("Duplicate")
else
inset data ----
end if
Posted
Updated 17-May-18 7:11am
Comments
Patrice T 8-Jul-17 1:30am    
What is the problem with your code.
Khabibb Mubarakk 8-Jul-17 7:37am    
doesn't work.
have u any idea..
please help me

1 solution

To check for duplicates, you need to check every existing row in the datagridview, from the piece of code, I suspect you check only the current one.
Use the debugger to see exactly what your code is doing.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]
Visual Basic / Visual Studio Video Tutorial - Basic Debugging - YouTube[^]
Visual Basic .NET programming for Beginners - Breakpoints and Debugging Tools[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Comments
Khabibb Mubarakk 8-Jul-17 9:08am    
hey guys i get solution this problem but i get another problem... i make it on private sub . this code cek the duplicate data on cells(nid_sp)

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
                If MessageBox.Show("Data Barang Sudah Ada , Tambah Jumlah Barang ?", "Confirmation !", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
                    dataspdibeli.Rows(i).Cells("QTY").Value = dataspdibeli.Rows(i).Cells("QTY").Value + Val(spqty.Text)
                Else
                    Exit Sub
                End If
                Exit Sub
            End If
        Next


this code to cek another condition before check duplicate data

<pre>Dim stock = datasparepart.CurrentRow.Cells(5).Value
        If stock = 0 Then
            MsgBox("Stock Item Habis", MsgBoxStyle.Exclamation)
        ElseIf stock < Val(spqty.Text) Then
            MsgBox("Stock Kurang Dari Jumlah Item Yang Dibeli", MsgBoxStyle.Exclamation)
        ElseIf spqty.Text = "" Then
            MsgBox("Masukkan Jumlah Item", MsgBoxStyle.Exclamation)
            spqty.Focus()
         
         elseif ----{HOW PLACE/CHECK THE PRIVATE SUB DUPLICATE HERE} 
  
        Else
            With dataspdibeli
                'Insert data ke DGV
                .Rows.Insert(.NewRowIndex, penfaktur.Text, spnid.Text, spnama.Text, spharga.Text, spqty.Text, spsubtotal.Text)
            End With
            hitung()
        End If
Patrice T 8-Jul-17 11:25am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Member 15484988 22-Mar-22 0:29am    
hi
I have a vb.net datagridview in which at column 0 there are duplicate dates.At column 2 I have various numbers.I want to find and to insert the smallest of these numbers for each duplicate date at column 3.
Please Help me
thanks
Patrice T 22-Mar-22 1:57am    

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900