2015-02-10 99 views
0

我认为这是常见问题,但我无法找到解决方案,所以我把这里,
我正在研究Windows应用程序项目,因为我需要得到雇员的详细信息,我有工资栏的限制应该是数字,所以应用此列的关键事件,但每当我尝试编辑datagridview中的员工地址其触发Keyup事件,我已被添加数字条件,所以我得到异常,只有当我在datagridview工资栏中输入工资时,我才需要调用此事件。

KeyUp事件触发DataGridview中的特定单元格c#

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

回答

1

您应该使用DataGridViewKeyUp事件来代替。
我会做这样的事情:

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

使用编辑控制往往是一个坏主意,并会导致你很多问题。

相关问题