dataGridView1 中有三列,
  Sum1,Sum2 ,Sum3
其中 Sum3=Sum1+Sum2
Sum1 < Sum2如何实现在Sum1中输入的值一定要小于Sum2,当Sum1中输入的值一定要大于Sum2会提示输入错误。

解决方案 »

  1.   

    这个是验证datagridview 单元里面只准输入数字,你参照这个自由发挥吧..#region 进入编辑状态时
            TextBox txtCell;
            private void dgvSettleInfo_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
            {
                txtCell = new TextBox();//单元格进入编辑状态时实列化文本框
                //单元格强制转化成文本框
                txtCell = (TextBox)e.Control;
                //绑定按键事件
                txtCell.KeyPress += new KeyPressEventHandler(txtCell_KeyPress);        } 
            #endregion#region 文本框的按键事件        private void txtCell_KeyPress(object sender,KeyPressEventArgs e)
            {
                try
                {
                    //输入的只准是数字,长度为7位数
                    if ((char.IsNumber(e.KeyChar)) && txtCell.Text.Length < 7)
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                    }
                    if ((e.KeyChar == (char)8))//允许输入退格
                    {
                        e.Handled = false;
                    }
                }
                catch (Exception ex)
                {                MessageBox.Show(ex.Message);
                }
            }        #endregion
      

  2.   

     //在DataGridView1_CellEndEdit中
    if (!DBNull.Value.Equals(dataGridView.CurrentRow.Cells["sum1"].Value) && !DBNull.Value.Equals(dataGridView.CurrentRow.Cells["sum2"].Value))
                {
    if(dataGridView.CurrentRow.Cells["sum1"].Value) < dataGridView.CurrentRow.Cells["sum2"].Value}
    {                dataGridView.CurrentRow.Cells[sum3].Value =dataGridView.CurrentRow.Cells["sum1"].Value) + dataGridView.CurrentRow.Cells[sum2].Value);
    else
    {
    messageBox.Show("错误");
    }            }