Sharanamma Jekeen
Sharanamma Jekeen

Reputation: 71

KeyUp event fire for only specific cell in datagridview c#

I think this is common issue but i am unable to find the solution so i am putting here,
I am working on windows application project, in that i need to get the employee details where i have restriction like salary column should be numeric so applied key up event for this column but whenever i'll try to edit employee address in the datagridview its firing Keyup event where i had been added numeric condition so i am getting exception,i have to call this event only if i am entering salary amount in datagridview salary column.

private void DataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (e.Control.GetType() == typeof(DataGridViewTextBoxEditingControl))
            {
                TextBox t = (TextBox)e.Control;
                if (DataGridView.CurrentCell.ColumnIndex == DataGridView.Columns["Salary"].Index)
                {
                    t.KeyUp += new KeyEventHandler(t_KeyUp);
                }
            }
        }


    private void t_KeyUp(object sender, KeyEventArgs e)
            {
                //CheckForInt = false;
                try
                {
                    //My Code Condition applies here
                }
                catch(Exception)
                {
               }
}

Upvotes: 0

Views: 3010

Answers (1)

Bioukh
Bioukh

Reputation: 1968

You should use the DataGridView's KeyUp event instead.
I would do something like that :

private void dataGridView1_KeyUp(object sender, KeyEventArgs e)
{
    if (dataGridView1.CurrentCell != null && dataGridView1.IsCurrentCellInEditMode && dataGridView1.CurrentCell.ColumnIndex == ColumnSalary.Index)
    {
        ...
    }
}

Using the editing control is often a bad idea and would lead you to many problems.

Upvotes: 1

Related Questions