Today I faced an issue that stole me a couple of hours of lazy work… My need was to intercept all the ‘.’ chars inputed by the user on a DataGridView cell and replace them with commas. With my big surprise, I have not found any suitable event of method.
After a coffebreak, I returned with clear mind and restored energies at the problem and I found this event on the DataGridView object:
EditingControlShowing
Neither the name nor the help tooltip of the intellisense help me so much understanding its behavior, but I registered to it and took a try.
Its DataGridViewEditingControlShowingEventArgs provide me with a Control object that is exactly the control used by the cell to handle input, a TextBox in my case. And now the magic.
I tried to treat the control as a normal TextBox, registering to its KeyPress event and it works!
myGrid.EditingControlShowing+=new DataGridViewEditingControlShowingEventHandler(myGrid_EditingControlShowing);
…
private void myGrid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) {
if (e.Control is TextBox) {
((TextBox)e.Control).KeyPress +=new KeyPressEventHandler(HandleKeyPress);
}
}private void HandleKeyPress(object sender, KeyPressEventArgs e) {
// My stuff here…
}
Another day at work, another .NETÂ trick learned…


