怎样获取DataGridView单元格中输入的数据(注:是单元格编辑未完成前的数据)

解决方案 »

  1.   

    因为我在dgv_M_CellLeave事件中获取的值是空的
    在dgv_M_CellEndEdit事件中可以获得,但这时系统已经报告用户录入的数据不符合类型,系统提示的内容他们根本看不明白。
      

  2.   

    DataGridView的编辑框Cell里,有一个EditingTextBox私有属性(DataGridViewTextBoxEditingControl类型,继承自TextBox,直接强转为TextBox即可),利用反射得到其实例,给Textchange事件绑上处理函数~~然后就可以了
      

  3.   


           TextBox cell;//初始化一个文本框
            private void dgv_M_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
           {
                  cell = new TextBox();
                  cell = (TextBox)e.Control;//把当前编辑的单元格转换成文本框
                    cell.KeyPress += new KeyPressEventHandler(cell_KeyPress);
           }         private void cell_KeyPress(object sender, KeyPressEventArgs e)
           {
                 //这里判断输入的每个字符
           }
      

  4.   

    private void dataGridView1_CellFormatting(object sender,
    System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
            {
                if (dataGridView1.Columns[e.ColumnIndex].Name.Equals("A"))
                {
                    Int32 intValue;
                    if (Int32.TryParse((String)e.Value, out intValue) &&
                        (intValue < 0))
                    {
                        e.CellStyle.BackColor = Color.Red;
                        e.CellStyle.SelectionBackColor = Color.DarkRed;
                    }
                }
    }
    单元格的验证可以使用dgv_details_CellValidating事件。
    验证不通过时调用e.Cancel = true;终止事件链,单元格将保持编辑状态。
    调用dgv_details.CancelEdit();可以使单元格的内容会滚到修改前的值。
    使用System.Windows.Forms.SendKeys.Send("^a");将全选单元格的内容。