在DataGridView中列数字的TextBox [英] Numeric TextBox in DataGridView column

查看:165
本文介绍了在DataGridView中列数字的TextBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DataGridView的。我想它的第一列或(在它里面有文本框)的任何所需的列是的 只有数字 的。我目前使用此代码:

 私人无效dataGridViewItems_EditingControlShowing(对象发件人,DataGridViewEditingControlShowingEventArgs E)
{
如果(dataGridViewItems.CurrentCell.ColumnIndex == dataGridViewItems.Columns [ITEMID]指数)
{
文本框的itemid = e.Control为文本框;
如果(!ITEMID = NULL)
{
itemID.KeyPress + =新KeyPressEventHandler(itemID_KeyPress);
}
}
}

私人无效itemID_KeyPress(对象发件人,KeyPressEventArgs E)
{
如果(!char.IsControl(E .KeyChar)
和;&安培;!char.IsDigit(e.KeyChar))
{
e.Handled = TRUE;
}
}

此代码的作品,但问题是,所有的文本框中的所有列只获得数字。


解决方案

我想通出自己:)



只是删除其中解决了我的问题的函数出发之前的事件



<$ p。 $ p> 私人无效dataGridViewItems_EditingControlShowing(对象发件人,DataGridViewEditingControlShowingEventArgs E)
{
e.Control.KeyPress - =新KeyPressEventHandler(itemID_KeyPress); //这行代码解决了我的问题
如果(dataGridViewItems.CurrentCell.ColumnIndex == dataGridViewItems.Columns [ITEMID]指数)
{
文本框的itemid = e.Control为文本框;
如果(!ITEMID = NULL)
{
itemID.KeyPress + =新KeyPressEventHandler(itemID_KeyPress);
}
}
}

私人无效itemID_KeyPress(对象发件人,KeyPressEventArgs E)
{
如果(!char.IsControl(E .KeyChar)
和;&安培;!char.IsDigit(e.KeyChar))
{
e.Handled = TRUE;
}
}


I have a DataGridView. I want its 1st column or any desired column (which has textboxes in it) to be NUMERIC ONLY. I am currently using this code:

private void dataGridViewItems_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (dataGridViewItems.CurrentCell.ColumnIndex == dataGridViewItems.Columns["itemID"].Index)
            {
                TextBox itemID = e.Control as TextBox;
                if (itemID != null)
                {
                    itemID.KeyPress += new KeyPressEventHandler(itemID_KeyPress);
                }
            }
        }

private void itemID_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
                && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

This code works but the problem is that all the textboxes in all the columns are getting numeric only.

解决方案

I figured it out myself :)

Just removed the previous events in the starting of the function which resolved my issue.

private void dataGridViewItems_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            e.Control.KeyPress -= new KeyPressEventHandler(itemID_KeyPress);//This line of code resolved my issue
            if (dataGridViewItems.CurrentCell.ColumnIndex == dataGridViewItems.Columns["itemID"].Index)
            {
                TextBox itemID = e.Control as TextBox;
                if (itemID != null)
                {
                    itemID.KeyPress += new KeyPressEventHandler(itemID_KeyPress);
                }
            }
        }

private void itemID_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
                && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

这篇关于在DataGridView中列数字的TextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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