怎么解决操作无效,原因是它导致对 SetCurrentCellAddressCore 函数的可重入调用问题

61 篇文章 1 订阅
56 篇文章 0 订阅

在开用Datagridview时处理keypress事件有时出现

操作无效,原因是它导致对 SetCurrentCellAddressCore 函数的可重入调用。


************** Exception Text **************
System.InvalidOperationException: 操作无效,原因是它导致对 SetCurrentCellAddressCore 函数的可重入调用。
   在 System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick)

解决方法如下

==============================================

 

public partial class DgvNumberOnlyColumn : Form

        {

            public DgvNumberOnlyColumn()

            {

                InitializeComponent();

            }

 

            private void DgvNumberOnlyColumn_Load(object sender, EventArgs e)

            {

                this.dataGridView1.Columns.Add("col1", "col1");

                this.dataGridView1.Columns.Add("col2", "col2");

                this.dataGridView1.Rows.Add();

                this.dataGridView1.EditingControlShowing += new

                     DataGridViewEditingControlShowingEventHandler(

                     dataGridView1_EditingControlShowing);

            }

 

            void dataGridView1_EditingControlShowing(object sender,

                 DataGridViewEditingControlShowingEventArgs e)

            {

                if (this.dataGridView1.CurrentCell.ColumnIndex == 0)

                {

                    if (e.Control is TextBox)

                    {

                        TextBox tb = e.Control as TextBox;

                        tb.KeyPress -= new KeyPressEventHandler(tb_KeyPress);

                        tb.KeyPress += new KeyPressEventHandler(tb_KeyPress);

                    }

                }

            }

 

            void tb_KeyPress(object sender, KeyPressEventArgs e)

            {

                if (!(char.IsDigit(e.KeyChar)))

                {

                    Keys key = (Keys)e.KeyChar;

 

                    if (!(key == Keys.Back || key == Keys.Delete))

                    {

                        e.Handled = true;

                    }

                }

            }

        }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值