Reputation: 775
I have a DataGridView
with AllowUserToAddRows
=true. The first column is an Id string which has to be unique. What's the best way to check that the Id the user entered hasn't been entered before?
Upvotes: 0
Views: 1226
Reputation: 73253
Hook into the CellEndEdit event, and in the handler use the event arguments to determine whether a) this is a new row and b) it's the ID column. If so, use a BackgroundWorker (or similar background technique) to query your data to see if this value is unique. If it isn't, then perhaps making a label visible to the user notifying them of the conflict is the least obtrusive course of action, and don't let the new row commit.
Upvotes: 1
Reputation: 52523
You could do an ajax call using jquery to check against values in your database. Or you could create the ID automatically for them using a GUID.
EDIT I just saw you're using windows forms... probably can't use jQuery for those :) I would go w/the GUID. You could also attach an onBlur event to the textbox and have it check your database to make sure it doesn't already exist.
Upvotes: 0