不执行KeyPress事件处理程序方法

时间:2013-12-28 05:52:28

标签: c# winforms datagridview

我为DataGridView KeyPress编写了以下代码,该代码未在VS 2010中执行:

private void DataGridView1_KeyPress(object sender, KeyPressEventArgs e)
    {
         MessageBox.Show("Entering DataGridView Key Press event");
    }

当我按下DataGridView的任何单元格中的键时,此方法不会被执行。谁能告诉我哪里可能错了?

我写了以下代码。代码仅在指定的单元格上执行,但即使按下数字键,它也始终执行else部分。对于单个按键事件,其他部分将执行两次。

private void MatCompDtlDataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        DataGridViewTextBoxEditingControl tb = (DataGridViewTextBoxEditingControl)e.Control;
        //tb.KeyPress += new KeyPressEventHandler(dataGridViewTextBox_KeyPress);
        e.Control.KeyPress += new KeyPressEventHandler(dataGridViewTextBox_KeyPress);
    }
    private void dataGridViewTextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
        DataGridViewRow CurrentRow = new DataGridViewRow();
        CurrentRow = MatCompDtlDataGridView.CurrentRow;
        if ((CurrentRow.Index != -1) && (MatCompDtlDataGridView.CurrentCell.ColumnIndex == txtQtyDGV.Index))
        {
            if (!char.IsControl(e.KeyChar)
            && !char.IsDigit(e.KeyChar)
            && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }
            else
            {
                e.Handled = false;
                MessageBox.Show("You can only enter Digits and a single Decimal Point. Maximum number of digits on the left side of the decimal point can be four and on the right hand side three");
            }
        }

3 个答案:

答案 0 :(得分:0)

在单元格中编辑内容时的按键事件不会通过DataGridView的KeyPress事件进行。您需要直接在编辑控件上处理KeyPress事件(您可以在EditingControlShowing事件中获取编辑控件)。或者,您可以从DataGridView派生并覆盖ProcessDialogKey方法,该方法将在编辑控件上调用关键事件。

Add the following line to form's constructor

public Form1()
{
   InitializeComponent();    
   dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);    
}

dataGridView1_EditingControlShowing event

void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
   dataGridView1.EditingControl.TextChanged += new EventHandler(EditingControl_TextChanged);
}

EditingControl_TextChanged event

void EditingControl_TextChanged(object sender, EventArgs e)
{
   //Capturing the value typed into grid's cell
   string text = dataGridView1.EditingControl.Text;
}

答案 1 :(得分:0)

确保您的DGV不是只读的,然后尝试类似:

  private void dataGridView1_EditingControlShowing(object sender, 
        DataGridViewEditingControlShowingEventArgs e)
    {
       MessageBox.Show("Entering DataGridView Key Press event");
  }

答案 2 :(得分:0)

您当前的验证逻辑看起来有点偏。通过仅检查当前按下的键,您无法确定单元格值是否为指定格式的有效十进制值。我认为以下样本更接近你想要的东西:

//only allow number and decimal separator
if(char.IsDigit(e.KeyChar) || e.KeyChar == '.')
{
    //curren text in cell
    var previousText = (sender as TextBox).Text;
    //cell content after the pressed key char appended
    var nextText = previousText + e.KeyChar.ToString();
    //validate cell content to make sure the format comply
    if(ValidateText(nextText)) e.Handled = true;
    else
    {
        //what to do if it doesn't comply
    }
}
else
{
    e.Handled = false;
    MessageBox.Show("You can only enter Digits and a single Decimal Point. Maximum number of digits on the left side of the decimal point can be four and on the right hand side three");
}

private bool ValidateText(string cellText)
{
    //TODO: put your validation logic here to ensure that..
    //Maximum number of digits on the left side of the decimal point can be four and on the right hand side three
}