I'm creating a custom Editing Control for a DataGridView which is based on TextBox. This works fine, except for the fact that (although I can override the OnValidating event) setting e.Cancel to true won't prevent the user from leaving the cell.
How can I force to stay in the editing control when user enters invalid data?
This must be possible, because when you enter invalid data in a bound column you have to implement Dataerror event and user is not able to leave cell while data is invalid.
I solved it by overriding the OnValidating event of the EditingControl to skip base.OnValidating. Added code in CellValidating of Grid which calls a custom Validation method of the underlying Editing Control, setting e.Cancel = true when validation fails.
Related
I want an example of Styles (or Templates) for a WPF DataGrid which will capture double click event on the DataGrid row and make it editable. When user leaves the row it should again get back to normal form (i.e. non editable)
One way that this could be done is via event handling in my view (code behind). However, that is not accepted as per MVVM principles. So I want ways to do this via Styles (or templates).
I am writting code in winforms using vb.net and I am trying to prevent a datagridview from losing focus when certain user business rule error conditions exist. When the errors exist I have code in the leave event which sets the focus back to itself. However this does not seem to work completely because the buttons on the form can still be clicked on. Does anyone know how to keep the focus on the datagridview and not allow the buttons to be clicked on without disabling the button when the user enters the datagridview? I am writting a custom datagridview control and want a general purpose routine for keeping the focus on the datagridview control and not allowing the buttons on the form from being able to be clicked on.
There is a certain chain of events that happen during the transition of focus from one control to another. There is a description of that in this msdn link.
I would recommend finding a way to make your business rules fit into that pattern, as this is the purpose for the validation hooks for the controls. Especially if you are creating your own control, also you may be able to encapsulate some of your validation logic using these events.
To prevent the form from changing focus on validation errors, set the form's AutoValidate property to EnablePreventFocusChange. http://msdn.microsoft.com/en-us/library/system.windows.forms.autovalidate(v=vs.100).aspx.
To prevent buttons from submitting when validation errors exist, set the button's CausesValidation property to true. http://msdn.microsoft.com/en-us/library/system.windows.forms.control.causesvalidation(v=vs.110).aspx
I have a WPF window that has a datacontext of my class 'Item'. When a user types in a TextBox the validation triggers with no issues. I need to validate this TextBox and every other Property that is set in this bound class when the user clicks 'Save'.
I don't believe UpdateSourceTrigger is what I'm looking for, although I have it set to PropertyChanged for when the user does actually do data entry on the field.
I would rather not set all my properties to the corresponding UI control on the Save click to force the update on the property but I could.
Thanks!
Data error validation only gets executed when binding pushes new property values out to the data source. If you want to execute that validation under any other circumstances, you have to write code to do it.
If your UI properties are bound to the item's properties, then the validation already should have run for each of them when the Save button is clicked. So why do you need to run it again?
One common issue is where you've set your bindings to update on LostFocus, but the object that the user clicks on to save the item isn't focusable. In this case, the last property updated in the UI isn't updated in the data source when the item is saved, since its control hasn't lost focus and its binding hasn't fired. Is that your problem?
I have a silverlight datagrid.
Validation works very well using DataAnnotations. However if I add a new row to the grid I don't get validation kicking in until I actuall try and edit cells.
Is there no way to force the grid to validate when a cell has not been clicked on?
Use DataGridTemplateColumn with TextBox and perform manual validation of item after you add it. See my question it is very related: Silverlight DataGrid validation show validation error for all objects|properties
I have a textbox that is bound (oneway) to a datatable that updates a couple of time a second.
So the textbox value continually reflects changes in datatable.
When I enter the textbox to manually set a value, the binding causes the value to continually be overwritten. How do I stop this?
When I have entered a value (textbox lost focus) I want the textbox to return to display the bound value and not the value I have just entered manually.
I had the same problem and I solve it by
With the BindingNavigator, I set it to null
bdNavProduct.BindingSource =null;
With other textbox control, datagridview, I clear the DataBinding
txtProductID.DataBindings.Clear();
txtProductName.DataBindings.Clear();
txtQuantity.DataBindings.Clear();
txtUnitPrice.DataBindings.Clear();
dgvProduct.DataBindings.Clear();
Tony
You could attach to the text input event and cancel the binding and then reapply it on the lost focus event.
I think however you need to consider why you are displaying your bound value in a text box at all? Would it not be more appropriate to have an uneditable Textblock displaying your database information with the editable Textbox separate for the optional user input.
While what you are asking is doable using the Textbox event I mentioned, it seems like it would be confusing from a user perspective.