简体   繁体   中英

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)
                {
               }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM