I'm using a bound UltraWinGrid to perform table data table entry.
I capture the dg_Error(object sender, ErrorEventArgs e) event to set a RowError msg string if the user tries to create a row with a duplicate key. However, after they correct the information and successfully add a new row to the dg and bound dt, the new row gets inserted showing the RowError msg I created in the dg_Error event.
What event should I use to clear the RowError msg on a new row or an updated row that previously had an error?
Hi Rob,
This is a tricky question to answer without seeing it in action. Off the top of my head, the AfterRowUpdate event seems like a reasonable choice. This event fires after the row has been committed to the underlying data source. So by that point, you can be sure that it can't possible have a duplicate key.
Thanks, that did the trick.
I should have thought of that event, but I assumed it would fire even if there was a data error on the row update. That would have effectively turned off the error msg set for the row in the dg_error event.
But, since I set the the "e.cancel = true;" in the dg_error event, it appears to stop the AfterRowUpdate from firing.
Thank you, that answers my question.
It seems like the best I can do is cancel the Update_Row and post an error message box telling the user to use the new row that is shown at the top of the grid, if they want to add a row. otherwise let them write over the existing keys.
Normally I wouldn't let a user touch the key fields in the first place. But I was told to create this grid for a "power user" who updates multiple tables in the database that do NOT have relationships defined on the database side. So she specifically takes care of finding and correcting any orphaned rows relationships in the other tables one at a time. Gives me the heebie-jeebies. I was just trying to help her if she meant to do an insert instead of an update in a controlled manner.
Rob Cline Sr said:I am doing that to flag data entry errors that don't meet the requirements for the column. However, how should I capture data entry that will cause key field issues (e.g. duplicate key).
IDataErrorInfo is implemented on the data source (typically) and so it there's so you can report additional errors that the data source itself doesn't already report. If you try to add a row with a duplicate key, it would (typically) throw an error and you would not be allowed to add the row in the first place, so there's no real reason to also mark that row with DataErrorInfo.
I'm not saying you can't do it, I'm just saying that this is not really the intended use of the interface. I could be wrong. :)
Regarding your second issue, this is going to be very complicated, if it's possible at all. You are really fighting hard against the basic principles of how DataBinding works here. Quite frankly, if I were you, I'd probably try to come up with a different way for users to handle this - like make them add a new row or modify an existing row explicitly. Even if you can get this to work the way you want, I would be very leary of unintended consequences or strange behaviors popping up down the road.
If you are at the point where you can determine that a new row needs to be added, then you can cancel the changes to the currently ActiveRow in the grid by calling:
this.ultraGrid1.ActiveRow.CancelUpdate();
I am doing that to flag data entry errors that don't meet the requirements for the column. However, how should I capture data entry that will cause key field issues (e.g. duplicate key).
Actually that brings up another issue I could not figure out how to solve. The grid I'm creating allows the user to update the two columns that form a composite key. I wanted to update the dt record if they didn't change the key columns, but attempt to create a new dt record if they entered a new valid composite field. If I do nothing the original dt record (i.e. the dr) get updated with a modified key and the the original record gets orphaned. I can easily detect what is about to happen in the BeforeRowUpdate event but I can't see how to cancel the Update event and fire a new row or insert row event instead using the current rows modified data.
Example:
columns are: key1, key2, data1, data2 (all columns can be editted)
data is:
1,1,"a","b"
1,2,"c","d"
2,1,"e","b"
2,2,"a","d"
row edit:
I select the second row (1,2,"c","d"); I modify the data so (1,3,"e","d")
What will happen is data record 2 of the dt will be overwritten with the modified data since this will not cause a constraint error in the dt. But, this will orphan any data associated with key pair 1,2.
What I can do is detect that the modified data should be added to the dt as a new row, what I can't seem to do is figure out any way to cancel the row update and instead perform an insert row with this modified data (i.e. 1,3,"e","d").
The event fires whenever the row is committed to the grid's DataSource. So it depends on the error. If it's an error like a duplicate key, which prevents the row from being added to the data source, then the event will not fire because the row cannot be committed.
BTW... DataRowInfo isn't typically used for this kind of error. It's usually used for errors in the data itself. For example, if a date field value is outside of the valid range (expired). In other words, errors in data that is valid as far as the data source is concerned, but invalid because something in the data fails to meet the requirements.