3
I have a Datagrid in which a certain column can only be inserted numbers, so I used "Editingcontrolshowing" to evaluate the user input in this column and only allow numbers.
string columnName = dataGridView_CP.CurrentCell.OwningColumn.Name;
IEnumerable<string> itemsNumero = new List<string> { "C_Altura" };
IEnumerable<string> itemsString = new List<string> { "Column_Id", "Column_Lote", "Column_Data" };
DataGridViewTextBoxEditingControl tb = (DataGridViewTextBoxEditingControl)e.Contro;
if (itemsNumero.Contains(columnName))
{
tb.KeyPress -= new KeyPressEventHandler(dataGridViewTextBox_KeyPress);
tb.KeyPress += new KeyPressEventHandler(dataGridViewTextBox_KeyPress);
tb.TextChanged -= new EventHandler(dataGridViewTextBox_TextChanged);
tb.TextChanged += new EventHandler(dataGridViewTextBox_TextChanged);
}
if (itemsString.Contains(columnName))
{
tb.KeyPress -= new KeyPressEventHandler(dataGridViewTextBox_KeyPress);
tb.TextChanged -= new EventHandler(dataGridViewTextBox_TextChanged);
}
It turns out that if the user enters to type something in that column the property works, but when he clicks on another cell to enter a data, and checking to type only numbers remains active. There is a way to disable this input before entering another cell?
The problem seems to be in the "Textchanged" that runs before the next cell enters her "Editingcontrolshowing".
I am actually selected the column. In my "if" if it is string column I remove the events, if it is number column I active. If I use only the event "Keypress" as you demonstrated works. The problem is that in addition to this I use the "Textchanged" that I use for when the user brings a text through cont+c, because even if I have put Keypress only numbers, if you paste a text it accepts. The problem is that after using a cell with number as soon as I click on another cell before Editingcontrolshowing is executed, and if the cell contains letter, it is deleted.
– Antonio Rafael da Silva Filho