Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check for Duplicate in DataGridView

Status
Not open for further replies.

Alexandrumbm

Programmer
Jun 20, 2007
13
0
0
RO
Hello to you all...

I have a DataGridView1 that takes values from an MDB.
I have a TextBox and a "Save" CommandButton to add new data to the database.

How do i prevent duplicates entry to the DatagridView ?!

Please help me!!!!!!

 
Usually you first of all put a constraint on the databse.

And then if you want to be nice to your users you do a select for the value in the textbox and if you get more then 0 result you know it is already in there and then you show a little messagebox to say that they shouldn't do that. If they do it correct you reward them.

Christiaan Baes
Belgium

"My old site" - Me
 
One of two ways I can think of:

1. Use the CellValidating event for the key column and check if the value already exists. This prevents the user trying to enter a duplicate.

2. Use DataView.ToTable when the user clicks the save button. This has a overload that returns a datatable containing distinct values of a specified column. If the distinct datatable has less rows than the datagridview, there are duplicates.
 
Do you have any sample about it ?! Please... i am not newb very newb to VB. I have search on many forums and i have googleit many many hours!

Please!!!
 
Very quick examples, please excuse any slack coding! Both assume that the distinct column in the source datatable is called DistinctCol and is of an integer type.

Example 1 (CellValidating event)
Code:
    Private Sub DataGridView1_CellValidating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating

        If DataGridView1.Columns(e.ColumnIndex).DataPropertyName = "DistinctCol" AndAlso e.FormattedValue.ToString() <> DataGridView1(e.ColumnIndex, e.RowIndex).Value.ToString() Then

            Dim dt As DataTable = CType(DataGridView1.DataSource, DataTable)

            Dim rows As DataRow() = dt.Select("DistinctCol = " & e.FormattedValue)

            If rows.Length > 0 Then

                MessageBox.Show("This value is already used.")

                e.Cancel = True

            End If

        End If

    End Sub
Example 2: (DataView.ToTable)
Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim dt As DataTable = CType(DataGridView1.DataSource, DataTable)

        Dim dv As New DataView(dt)

        Dim distinct As DataTable = dv.ToTable(True, New String() {"DistinctCol"})

        If distinct.Rows.Count <> dt.Rows.Count Then

            MessageBox.Show("There are duplicate values.")

        End If

    End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top