让一个DataGridView细胞只有一些字符键入 [英] Let only some chars be typed in a datagridview cell

查看:136
本文介绍了让一个DataGridView细胞只有一些字符键入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让只有特定的字符被添加到一个DataGridView细胞? 像'1234567890'?

Is there a way to let only certain chars be added to a datagridview cell? like '1234567890'?

推荐答案

有两种方法,我知道的,你可以使用这个。第一个(我认为是最好的)是使用上的的DataGridView的CellValidating事件并检查输入的文本的数字。

There are two approaches I know of that you can use for this. The first (and I think the best) is to use the CellValidating event on the DataGridView and check if the entered text is numeric.

下面是这里面设置行误差值过一个例子(一个额外的CellEndEdit事件处理程序以防用户取消了编辑)。

Here is an example of that which sets the row error value too (with an additional CellEndEdit event handler incase the user cancels out of editing).

private void dataGridView1_CellValidating(object sender,
        DataGridViewCellValidatingEventArgs e)
    {
        string headerText = 
            dataGridView1.Columns[e.ColumnIndex].HeaderText;

        // Abort validation if cell is not in the Age column.
        if (!headerText.Equals("Age")) return;

        int output;

        // Confirm that the cell is an integer.
        if (!int.TryParse(e.FormattedValue.ToString(), out output))
        {
            dataGridView1.Rows[e.RowIndex].ErrorText =
                "Age must be numeric";
            e.Cancel = true;
        }

    }

    void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        // Clear the row error in case the user presses ESC.   
        dataGridView1.Rows[e.RowIndex].ErrorText = String.Empty;
    }

第二种方法是使用EditingControlShowing事件和附加一个事件到细胞的主要preSS - 我不是这种方法的这样一个风扇,因为非数字键就静静地阻挡输入 - 尽管我想你可以给一些反馈(如钟发声),它只是感觉比其他方式更多的工作。

The second approach is to use the EditingControlShowing event and the attach an event to the cell's KeyPress - I'm not such a fan of this approach since it silently blocks the input of non numeric keys - though I suppose you could give some feedback (like a bell sounding) it just feels like more work compared to the other way.

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    e.Control.KeyPress -= TextboxNumeric_KeyPress;
    if ((int)(((System.Windows.Forms.DataGridView)(sender)).CurrentCell.ColumnIndex) == 1)
    {
         e.Control.KeyPress += TextboxNumeric_KeyPress;
    }
}

private void TextboxNumeric_KeyPress(object sender, KeyPressEventArgs e)
{
    bool nonNumberEntered = true;

    if ((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 8)
    {
        nonNumberEntered = false;
    }

    if (nonNumberEntered)
     {
        // Stop the character from being entered into the control since it is non-numerical.
        e.Handled = true;
    }
    else
    {
        e.Handled = false;
    }
}

这一个重要的一点是要小心删除的事件处理程序上的编辑控件显示方式中的控制。这是重要的,因为的DataGridView 重用为同一类型的每个单元,包括在不同的列相同的对象。如果将一个事件处理程序,以控制在一个文本框列,其他所有的文本框单元网格将具有相同的处理程序!此外,多个处理程序附会,一个控制显示在每个时间。

One important note with this is be careful to remove the event handler on the control within the editing control showing method. This is important since the DataGridView reuses the same object for each cell of the same type, including across different columns. If you attach an event handler to a control in one textbox column, all the other text box cells in the grid will have the same handler! Also, multiple handlers will be attached, one for each time the control is shown.

第一个解决方案来自这个MSDN文章。第二个来自这个博客

The first solution came from this MSDN article. The second came from this blog.

这篇关于让一个DataGridView细胞只有一些字符键入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆