CSDN不能连续回复3次,我只能重新开个一张贴了,烦,明天要检查了,大家快快看看该怎么改,谢谢!
第4列的数据是用键盘输入的,然后与第3列数据进行比较,下面是我自己写的,但发现还是会出现第四列对象没有初始化的问题,请问我该怎么改,或者有别的方法能实现吗?谢谢!!
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            bool flag = false;
            if (e.ColumnIndex == 4)
            {
                try
                {
                    if (!String.IsNullOrEmpty(e.FormattedValue.ToString()))
                    {
                        flag = true;
                        decimal val = decimal.Parse(e.FormattedValue.ToString());                    }
                }
                catch (Exception ex)
                {
                    flag = false;
                    dataGridView1.Rows[e.RowIndex].ErrorText = "必须输入数字";
                    MessageBox.Show("必须输入数字");
                    e.Cancel = true;                }
            
                if (flag&&e.FormattedValue.ToString().Trim()!=null)
                {
                                       for (int i = 0; i < dataGridView1.Rows.Count; i++)
                    {
                        decimal d1 = Convert.ToDecimal(dataGridView1.Rows[i].Cells[4].Value.ToString());
                        decimal d2 = Convert.ToDecimal(dataGridView1.Rows[i].Cells[3].Value.ToString());
                        if (d1 > d2)
                        {
                            dataGridView1.Rows[i].ErrorText = "实际入库量不能大于订购量";
                            MessageBox.Show("实际入库量不能大于订购量");
                            e.Cancel = true;
                        }
                    }
                }            }
        } 

解决方案 »

  1.   

    第一.你要把错误的信息贴出来.
    第二.Cell数组的下标是从0开始的,第四列就是Cells[3],第三列就是Cells[2],看看有没有弄错.
      

  2.   

    第一.你要把错误的信息贴出来.
    第二.Cell数组的下标是从0开始的,第四列就是Cells[3],第三列就是Cells[2],看看有没有弄错.
      

  3.   

    第一.你要把错误的信息贴出来.
    第二.Cell数组的下标是从0开始的,第四列就是Cells[3],第三列就是Cells[2],看看有没有弄错.
      

  4.   

     if (e.ColumnIndex == 4)?
    你要比哪列?
      

  5.   

    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
            {
               
                if (!DBNull.Value.Equals(dataGridView1.CurrentRow.Cells[2].Value) & !DBNull.Value.Equals(dataGridView1.CurrentRow.Cells[3].Value))
                {
                    if (decimal.Parse(dataGridView1.CurrentRow.Cells[2].Value.ToString()) > decimal.Parse(dataGridView1.CurrentRow.Cells[3].Value.ToString()))
                    {
                        MessageBox.Show("实际入库量不能大于订购量");                }                
                }          }
      

  6.   

    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
            {
               
                if (!DBNull.Value.Equals(dataGridView1.CurrentRow.Cells[2].Value) & !DBNull.Value.Equals(dataGridView1.CurrentRow.Cells[3].Value))
                {
                    if (decimal.Parse(dataGridView1.CurrentRow.Cells[2].Value.ToString()) > decimal.Parse(dataGridView1.CurrentRow.Cells[3].Value.ToString()))
                    {
                        MessageBox.Show("实际入库量不能大于订购量");                }                
                }          }
      

  7.   

    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
            {
               
                if (!DBNull.Value.Equals(dataGridView1.CurrentRow.Cells[2].Value) & !DBNull.Value.Equals(dataGridView1.CurrentRow.Cells[3].Value))
                {
                    if (decimal.Parse(dataGridView1.CurrentRow.Cells[2].Value.ToString()) > decimal.Parse(dataGridView1.CurrentRow.Cells[3].Value.ToString()))
                    {
                        MessageBox.Show("实际入库量不能大于订购量");                }                
                }          }
      

  8.   

    这是按你34列的要把cells[2]和cells[3]里的数改为3、4,我想你不会笨到了照抄上面的吧
      

  9.   

    在比较前用一次datagridview.EndEdit()
      

  10.   

    在比较前用一次datagridview.EndEdit()