【问题标题】:KeyUp event fire for only specific cell in datagridview c#KeyUp 事件仅针对 datagridview c# 中的特定单元格触发
【发布时间】:2015-02-10 04:46:12
【问题描述】:

我认为这是常见问题,但我无法找到解决方案,所以我放在这里,
我正在处理 Windows 应用程序项目,因为我需要获取我有限制的员工详细信息,例如薪水列应该是数字,因此为此列应用了键向上事件,但是每当我尝试在 datagridview 中编辑员工地址时,它的触发我已经添加了数字条件的 Keyup 事件,所以我得到了异常,只有当我在 datagridview 工资列中输入工资金额时,我才必须调用此事件。

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

【问题讨论】:

    标签: c# winforms datagridview


    【解决方案1】:

    您应该改用DataGridViewKeyUp 事件。
    我会做这样的事情:

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

    使用编辑控件通常是个坏主意,会导致很多问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-11
      • 2014-12-30
      • 1970-01-01
      • 2012-11-06
      • 2018-12-08
      相关资源
      最近更新 更多