Undo Editingcontrolshowing

Asked

Viewed 44 times

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".

1 answer

1

The problem is because you arrow the event to all columns, and you should set the event only in the columns you want to consist of, you can do this by taking the index of the column you want to consist of, see this code that I already use in some of my applications:

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    // Aqui pega o index da coluna que está selecionada.
    int colIndex = this.dataGridView1.CurrentCell.ColumnIndex;

    // Aqui eu verifico se a coluna que está selecionada é a coluna que vou consistir a entrada de dados.
    if (colIndex == 0)
        e.Control.KeyPress += dataGridView1_KeyPress;
    else
        e.Control.KeyPress -= dataGridView1_KeyPress;
}

private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)
{
    // Aqui eu só permito a informação de números
    e.Handled = !(Char.IsNumber(e.KeyChar) || e.KeyChar == 8);
}
  • 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.