.NET Forum / Windows Forms / WinForm General / April 2005
Binding property with CancelCurrentEdit
|
|
Thread rating:  |
Marina - 05 Apr 2005 16:00 GMT Hi,
I have a text control bound to a column that is of type Decimal. I am doing the validation myself to see if the user typed in something invalid, calling CancelCurrentEdit and cancelling the validation event.
If the user typed in something that is not a number, such as 'abc', then doing this works. The old value stored in the data source is popped into the control.
However, if the user typed in something that is a legal number, but outside the set of legal values as I have determined, and I try the exact same thing, the data source retains the old value, but the control is not refreshed to match the datasource. So if the user types in '11', but due to business rules this is invalid, and I would like to cancel the edit, the datasource retains it's previous value, but the input control on the screen still says '11'.
What is going on here?
Any ideas on how to fix this? Other then manually changing the text of the control, which I would like to avoid since it will mean reparsing the item, and committing it as a change to the row - when in fact no change took place.
Cor Ligthert - 05 Apr 2005 16:22 GMT Marina,
You know that I gladly like to help especially you.
However you are for me very mystorious about the type of control. I get the idea it is a windowsform datagrid, however I am not sure.
Cor
Marina - 05 Apr 2005 18:34 GMT :) Thanks I said it was a text control. It happens to be an infragistics UltraTextEditor, but I dont' think it is related to it being an infragistics control, as it would have no concept of me trying to deal with a numeric. I think it is related to the binding being to a DataColumn the datatype of which is Decimal.
Basically, if what the user entered cannot be interpreted by .net as a numeric, CancelCurrentEdit puts the old value in the control. However, if .net can turn what is typed in as a numeric, CancelCurrentEdit leaves it there for some reason.
Any insights?
> Marina, > [quoted text clipped - 4 lines] > > Cor Cor Ligthert - 05 Apr 2005 19:06 GMT Marina,
I ask this because I don't hear you speaking about the binding events, which seems to me very easy for the problem you describe.
I assume you know them, however you never know.
When you use normal single databinding with the bindingmanager and not a kind of repeater than you can do everything with it.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlr fSystemWindowsFormsBindingEventsTopic.asp
When you know this already, than I go another route, however when I had not showed it than it would have been a waste. I have seen more people long time active in this newsgroups who never saw them until I showed them. (you have to push on format and parse to see the documentation).
Cor
Marina - 05 Apr 2005 19:33 GMT Yes, I am handling those 2 events already.
In the Parse, I am keeping track of whether or not the value is acceptable. The value may be unacceptable because it cannot be parsed to a numeric, or because it is simply a business rule that is making the value not valid.
In the Validate event for the control, I am checking this saved status. If the value is good, I call EndCurrentEdit. Otherwise I call CancelCurrentEdit and say e.Cancel=True, to cancel the event and keep the user in the textbox.
Now, if the status of the parse is invalid due to the string not being parsed to a numeric, CancelCurrentEdit replaces the old value in the control. If it is due to a business rule, CancelCurrentEdit leaves the original value in the datasource, but also leaves the 'bad' value in the control, instead of putting the datasource's value in the control.
Is that a better explanation of my issue?
> Marina, > [quoted text clipped - 14 lines] > > Cor Cor Ligthert - 05 Apr 2005 19:46 GMT Marina,
A little bit to complex now :-) I think I do this tomorrow. When you found it, can you message that than?
Cor
> In the Parse, I am keeping track of whether or not the value is > acceptable. The value may be unacceptable because it cannot be parsed to a [quoted text clipped - 32 lines] >> >> Cor Marina - 05 Apr 2005 19:51 GMT Ok.
I can fix this, if after CancelCurrentEdit, I just replace the text of the control with whatever is in the datasource. It just seems like I shouldn't have to in this case.
> Marina, > [quoted text clipped - 40 lines] >>> >>> Cor Cor Ligthert - 06 Apr 2005 12:13 GMT Marina,
Can a datarow.rejectchanges not be the answer on your question. (First copied to get the changed values).
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlr fsystemdatadatarowclassrejectchangestopic.asp
Just an idea.
Cor
Marina - 06 Apr 2005 16:43 GMT No, that doesn't work.
For example, let's say the column starts out at the value 2. Then, someone enters a 5. This is valid. Before saving the row, the user goes in and enters 11. Now, 11 is not a valid value. Calling RejectChanges, sets the value back to 2, instead of back to 5.
The desired behavior, would be for the value to go back to 5. And I can't call AcceptChanges in between, as then the row would get a state of not modified, and there would be no way to tell there were any changes to the row.
> Marina, > [quoted text clipped - 6 lines] > > Cor Cor Ligthert - 06 Apr 2005 17:04 GMT Marina,
I readed it again.
What you want seems to be for me that you want to do the binding again, so that the correct values from the source are in your control after that all validating is done and you have corrected the datasource accoording to that.
Is that correct?
Cor
Marina - 06 Apr 2005 19:19 GMT Right.
It does it on its own if the bad value cannot be interpreted as a numeric at all. But if the value is bad because business rule determine so, then the control behaves differently.
This is the part I don't understand. The exact same code is executing - just what the user typed in is different.
> Marina, > [quoted text clipped - 8 lines] > > Cor Cor Ligthert - 06 Apr 2005 19:37 GMT Marina,
I thought that your answer would have been. I have already removed the binding and added it again.
:-) Cor
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|