DataGridView: validating rows for duplicate

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Faelenor
    New Member
    • Nov 2008
    • 1

    #1

    DataGridView: validating rows for duplicate

    Hi,

    I have a DataGridView bounded to a database table. I'm trying to validate a row entered by a user with the RowValidating event. What i want to do is check if the user entered a value already in the database. The problem is that when the RowValidating event is fired, the IsNewRow property is already set to false, but the value is not in the DataTable yet.

    For example, if the user adds a new row in the database, I check in my DataTable if the value is already there. If so, I show an error message. But if the user doesn't create a new row and renames one with an already existing value, the error message will pop because the value is already there.

    That's why I tried to use the IsNewRow property, to know if we're dealing with a new row or just an edit. But the value is always false, event when I'm adding a new row.

    So, how can I know that the user is add a new row? And is there a better way to check for duplicate?

    Thank you!
  • dip_developer
    Recognized Expert Contributor
    • Aug 2006
    • 648

    #2
    i can just give you a logical way.....because i dont know how you are managing your insert /updates........ ...

    take a flag(a variable)...to hold the edit/insert status.......wh enever user creats a new row in the grid(by clicking a button or so) set the flag value to 1 or "insert" or as you want......other wise the flag value will be 0 or "update" or as you want........... .

    Dim flag As String=""

    whenever user creates a new row set
    flag="insert"

    now you have to play in the SQL Query by checking the flag........... .

    If flag="insert" Then
    'check for the duplicate record
    else
    'check for the duplicate record leaving the current record which is being edited
    End If

    Comment

    Working...