Search This Blog

Monday, April 22, 2013

Numeric Column DataGridView c#

C# DataGridView > Numeric Column

EditingControlShowing event occurs when a control for editing a cell is showing.

Example


         private void dataGridView1_EditingControlShowing(object sender,                  DataGridViewEditingControlShowingEventArgs e)
        {
            e.Control.KeyPress -= new KeyPressEventHandler(NumColumn_KeyPress);
            if (dataGridView1.CurrentCell.ColumnIndex == 2 || dataGridView1.CurrentCell.ColumnIndex == 3) // numeric columns
            {
                TextBox tb = e.Control as TextBox;
                if (tb != null)
                {
                    tb.KeyPress += new KeyPressEventHandler(NumColumn_KeyPress);
                }
            }
        }
        private void NumColumn_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
                && !char.IsDigit(e.KeyChar) && !char.IsPunctuation(e.KeyChar) )
            {
                e.Handled = true;
            }


        }