Rank: Newbie Groups: Member
Joined: 10/6/2009 Posts: 7
|
Having added a row to a Grid control I validate the input back at the server before submitting it to my DB. If I detect a user data entry error I report the problem allowing the user to rectify the issue. During the postback I prevent the grid from getting Bound again so that the users entries are still visible.
The problem comes when the user submits the changes a second time. The AddedItems collection now has a length of zero and the new item now appears under ChangedItems.
I can work around this problem but I am interested to know whether I am approaching this issue correctly since I plan to use the same approach in many other situations and do not wish to perpetuate bad practices!
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,194
|
Hi,
This behavior is by design. The Grid doesn't maintain any information about whether the data has been rejected or saved. It sticks to a simple rule: the first time the data is provided to you it's "Added". Afterwards it is "Changed".
The easiest way to address such scenario is to use GridItem.Key property. Assume that your DB has a key field, you can set the Grid's KeyField to the name of the key field. That instructs the Grid to automatically populate the key value into each item's GridItem.Key property during data binding. For newly added item, GridItem.Key should be null. However once you save a new item to the DB, you should get back the key value and store it update GridItem.Key accordingly, whereas rejected item’s Key value stay as null. This way you would simply check both AddedItems and ChangedItems every time, but purely rely on whether the item has a key value to determine whether it is a new item or not.
Please let us know if this makes sense to you.
Thanks!
|
Rank: Newbie Groups: Member
Joined: 10/6/2009 Posts: 7
|
That makes perfect sense, in fact that's precisely what I had done. Now that I know I'm on the right track I'll leave you alone for a while!
Great looking product, I look forward to learning more.
Thanks again.
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,194
|
Glad to hear that you like our product. Please feel free to let us know if you have any more questions.
|