cara mencegah entri data ganda dalam datagridview

 

 

 

Imports System.Data.SqlClient

 

Public Class CegahdataGanda

 

Dim conn As SqlConnection
Dim cmd As SqlCommand
Dim dr As SqlDataReader

 

Sub koneksi()
conn = New SqlConnection("data source=user-pc\sqlexpress;initial catalog=dbnashir;integrated security=true")
conn.Open()
End Sub

 

Private Sub DGV_CellEndEdit (ByVal sender As Object, ByVal e As System. Windows. Forms. DataGridViewCellEventArgs) Handles DGV.CellEndEdit

If e.ColumnIndex = 0 Then
'======================================
For barisatas As Integer = 0 To DGV.RowCount - 1
For barisbawah = barisatas + 1 To DGV.RowCount - 1
If DGV.Rows(barisbawah).Cells(0).Value = DGV.Rows(barisatas).Cells(0).Value Then
DGV.Rows(barisatas).Cells(3).Value = DGV.Rows(barisatas).Cells(3).Value + 1
DGV.Rows(barisatas).Cells(4).Value = DGV.Rows(barisatas).Cells(2).Value * DGV.Rows(barisatas).Cells(3).Value
DGV.Rows.RemoveAt(DGV.CurrentCell.RowIndex)
SendKeys.Send("{down}")
Exit Sub
End If
Next
Next
'=======================================
Call koneksi()
cmd = New SqlCommand("select * from tblbarang where kode_barang='" & DGV.Rows(e.RowIndex).Cells(0).Value & "'", conn)
dr = cmd.ExecuteReader
dr.Read()
If dr.HasRows Then
DGV.Rows(e.RowIndex).Cells(1).Value = dr.Item(1)
DGV.Rows(e.RowIndex).Cells(2).Value = dr.Item(3)
DGV.Rows(e.RowIndex).Cells(3).Value = 1
DGV.Rows(e.RowIndex).Cells(4).Value = DGV.Rows(e.RowIndex).Cells(2).Value * DGV.Rows(e.RowIndex).Cells(3).Value
Else
MsgBox("data tidak ditemukan")
SendKeys.Send("{UP}")
DGV.Rows(e.RowIndex).Cells(0).Value = ""
End If
End If
End Sub
End Class