Raise CellValueChanged from custom edit control in DataGridView

I have a DataGridView (WinForms) in which I defined a custom EditingControl derived from a DataGridViewTextBoxEditingControl that only accepts numeric characters.

I need to raise the CellValueChanged event in the DataGridView every time the user presses a key, but the default behavior is to raise the event only after the release is complete.

How can I raise an event every time a key is pressed?

public class DataGridViewNumericTextBoxEditingControl : DataGridViewTextBoxEditingControl { protected override void OnKeyPress(KeyPressEventArgs e) { e.Handled = Char.IsLetter(e.KeyChar); if (!e.Handled) RaiseDataGridViewCellValueChanged(); // <-- Any way? } } 

Update:

I found a workaround, but I'm not sure if this is a good solution:

 public class DataGridViewNumericTextBoxEditingControl : DataGridViewTextBoxEditingControl { protected override void OnKeyPress(KeyPressEventArgs e) { e.Handled = Char.IsLetter(e.KeyChar); if (!e.Handled) { EditingControlDataGridView.EndEdit(); EditingControlDataGridView.BeginEdit(false); } } } 
+4
source share
1 answer

What is the purpose here? It seems you are trying to create a keyboard mask where any non-confirming character triggers a warning? If so, you can find happiness in the DataGridView by adding MaskedTextBoxColumn. MaskedTextBoxColumn controls input in a much smarter way than force editing.

See here the code that shows how to use it: http://msdn.microsoft.com/en-us/library/ms180996.aspx

According to your updated criteria in the comments, it looks like your solution is probably as good as it will be. Understand, however, that it is very easy to get into endless event loops when you have other controls updated on another, based on keyboard events. You are probably best off having one delegate in a form that handles all events, as this can prevent endless event trigger cycles by throwing false events based on state.

Effectively this will use the View Viewer Model template, where your delegate acts as the host and controls the traffic in your view (your user interface).

By doing this, you can even move the Presenter logic to a new class with which you then bind the data, which allows you to fully control the user interface from a single logical construction, rather than a mishmash of event handlers.

+1
source

All Articles