Reputation: 1
I have a gridcontrol bind to Datatable, in my grid i let users to select products for proccessing.
The issue is I don't want users to add same product code twice to the grid.
Is there a way to prevent them adding the same product code twice?
I already tried Unique attirbute in my DataTable bu it didn't quite worked as I needed.
Any help will be appreciated
Upvotes: 0
Views: 1403
Reputation: 81
I normally use GridView.ValidateRow event which should fire when the user has finished editing a row. Event args provides access to the row object via e.Row, an error message via e.ErrorText, and valid status via e.Valid, so it should be a simple matter of grabbing the product code from the updated row and looking for it in the datatable.
Upvotes: 1
Reputation: 152
I thought of something like this
gridViewDefault.RowLoaded += gridViewDefault_RowLoaded;
Hashtable hashTable = new Hashtable();
void gridViewDefault_RowLoaded(object sender, RowEventArgs e)
{
var obj = ( sender as GridView ).GetRow(e.RowHandle) as OrderDocument;
var uniqueId = obj.Document.DocumentId;
if( hashTable[uniqueId] == null )
hashTable.Add(uniqueId, 1);
else
( sender as GridView ).DeleteRow(e.RowHandle);
}
but then saw that the RowLoaded event only fires in Instant Feedback Mode
Apparently there is no immediate event after a DataSource has finised Loading.
Therefore, instead of event-based, I would go for a function similar to the one I wrote, which Iterates the whole GridView, after you have all the Data.
Upvotes: 0