datagridview的某个单元格,在编辑模式下,有没有方法实时判断每个输入字符?不要_CellBeginEdit,_CellEndEdit这俩,在这两个的中间!!!

解决方案 »

  1.   

    在编译模式下,你可以设置一个相应函数在cs文件当中,当单击UPdate时执行相应的检查操作。如果满足要求,则编辑成功,不满足要求则重新返回。
      

  2.   

    CellMouseUp事件中 KeyCode
      

  3.   


    private void dataGridView1_CellValidating(object sender,
        DataGridViewCellValidatingEventArgs e)
    {
        dataGridView1.Rows[e.RowIndex].ErrorText = "";
        int newInteger;    // Don't try to validate the 'new row' until finished 
        // editing since there
        // is not any point in validating its initial value.
        if (dataGridView1.Rows[e.RowIndex].IsNewRow) { return; }
        if (!int.TryParse(e.FormattedValue.ToString(),
            out newInteger) || newInteger < 0)
        {
            e.Cancel = true;
            dataGridView1.Rows[e.RowIndex].ErrorText = "the value must be a non-negative integer";
        }
    }下面的代码示例处理 CellValidating 事件,以确保用户仅输入正整数。来自csdn
      

  4.   


        public partial class Form1 : Form
        {
            //声明计数器
            private int count = 0;        /// <summary>
            /// 初始化窗体
            /// </summary>
            public Form1()
            {
                InitializeComponent();
            }        /// <summary>
            /// 单元格值变化的事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
            {
                count++;
                //当字符数达到3个时 弹出提示窗口
                if (count == 3)
                {
                    MessageBox.Show("");
                    //还原计数器
                    count = 0;
                    return;
                }
            }        /// <summary>
            /// 当单元格的内容已更改,但更改尚未保存时,该单元格将标记为已修改
            /// 个人理解:
            /// 当单元格进入编辑状态且值发生变化时 触发此事件 并完成一次验证事件后重新进入编辑状态
            /// 所以当每输入一次时就会触发CellValueChanged事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
            {
                if (dataGridView1.IsCurrentCellDirty)
                {
                    dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
                }
                
            }
        }