Hi Guys,

I need Some help in validating a particular coloumn in datagrid for duplicate values before inserting into database.Im using datatable.
Please help with some example code.appreciate our help.
regards,
vamsee

Use this.

Dim duplicates = dt.AsEnumerable().GroupBy(Function(i) i.Field(Of String)("Col1")).Where(Function(g) g.Count() > 1).Select(Function(g) g.Key)

        For Each ed In duplicates
            Console.WriteLine(ed)
        Next

Hope it will help you. or
if u want to manipulate or process datatable for duplicate rows you can follow as same code

int[] listOfItems = new[] { 4, 2, 3, 1, 6, 4, 3 };
var duplicates = listOfItems
    .GroupBy(i => i)
    .Where(g => g.Count() > 1)
    .Select(g => g.Key);
foreach (var d in duplicates)
    Console.WriteLine(d);

Edited 5 Years Ago by bhagawatshinde: n/a

Hi,
Thanks for ur reply.,
Where shld i put the above code?Whn im trying to put this in cellvalidated function,im getting a run time exception.please let me know.thanks.

and also it shld display an error message like "duplicate value inserted".,
thanks very much for ur help.

Hi Bhagvat,
Need you help here please..,where to put the above codes?i have tried placing it in validation sub,but getting an error,,
my requirement is simple:
I have a plot column in my datagrid.,users wil be entering plot numbers,whn ever they enter a plot no which they have already entered an error shld be displayed to them..
please help,thanks

Hi vammy,
You require the repetation on keypress on cell or simple way to write for loop code on button to check cell repetation values.

Hi Bhagvat,
How do i do that?Im new to vb.net.Could u please assist me with some code?thanks..

take button name 'Check Repetition' on click event write the following code

For k = 0 To dtgtest.RowCount - 1
                Dim str As String = dtgtest.Item(0, k).Value
                Dim str1 As String = dtgtest.Item(0, k + 1).Value
                If str = str1 Then
                    MessageBox.Show("Repeat value Found in row " & k + 1)
                End If
            Next

this code is not tested it will work hope so if no then cooming with solution wait

Edited 5 Years Ago by bhagawatshinde: n/a

Try this

Try
            Dim arr As New ArrayList
            For k = 0 To dtgtest.RowCount - 1
                If Not arr.Contains(dtgtest.Item(0, k).Value) Then
                    arr.Add(dtgtest.Item(0, k).Value)
                Else
                    MessageBox.Show("Repetation found in row " & k + 1)
                End If
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

thts fantastic brother..!:)
got exactly what i wanted..,thank you so much..god bless..

This question has already been answered. Start a new discussion instead.