views:

73

answers:

2

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?

A: 

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.

Jason
+1  A: 

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.

Stuart Dunkeld