Search code examples
c#winformsdatagridviewkeypresscell-formatting

datagridview1_keypress does not working


        private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (dataGridView1.CurrentCell.ColumnIndex == 5 || dataGridView1.CurrentCell.ColumnIndex == 10)
        {
            aTimer.Interval = 850;
            aTimer.Start();
            string pwd = "*";
            pass.Add(e.KeyChar);
            dataGridView1.CurrentCell.Value = pwd;
            dataGridView1.CurrentCell.Value = GridView1_PreRender1(dataGridView1.CurrentCell.Value.ToString());

        }
    }

I'm getting no error but at the runtime which ever grid cell I click and enter values KeyPress does not take any action and it's killing me. I'm too desperate, any help would be great.


Solution

  • The issue is related to the fact that once you start editing a cell's value, you're no longer within the DataGridView but within the "Editing Control" defined by the cell's owning column. For a DataGridViewTextBoxColumn this is a TextBox. You'll need to hook into the events of the editing control. The details of which are shared in another answer: