日期:2014-05-18  浏览次数:21008 次

dataGridView数据比较问题
 
  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;
  }
  }
  }

  }
  }

 

------解决方案--------------------
C# code
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("实际入库量不能大于订购量");

                }

                
            }  

        }