for (int i = 0; i < dataGridView1.Rows.Count; i++)
                   {                       if (Convert.ToInt32(dataGridView1.Rows[i].Cells[4].Value.ToString()) > Convert.ToInt32(dataGridView1.Rows[i].Cells[3].Value.ToString()))
                        {
                           dataGridView1.Rows[i].ErrorText = "实际入库量不能大于订购量";
                           MessageBox.Show("实际入库量不能大于订购量");
                            e.Cancel = true;
                        }
                   }我想比较3、4列两个单元的值,但这样写不行,说什么对象没初始化,请问该怎么改或者有什么更好的方法实现,O(∩_∩)O谢谢!!
                

解决方案 »

  1.   

    string s1 = dataGridView1.Rows[i].Cells[4].Value.ToString();
    string s2 = dataGridView1.Rows[i].Cells[3].Value.ToString();
    如果是数值型的,最好转成数值再比较,比较真实,字符串的大小比较有时候可能结果不是
    你想要的,
    decimal d1 = (decimal)dataGridView1.Rows[i].Cells[4].Value.ToString();
    decimal d2 = (decimal)dataGridView1.Rows[i].Cells[3].Value.ToString();对象没有初始化,有可能是在你写的代码的事件加载时,dataGridView1尚没有数据.
      

  2.   

    在转换前你得确认 获取的值是不是为空啊,然后再进行转换,不然会出错下面这样就是转换失败的时候值为0double d1=0.0;
    double.TryParse(dataGridView1.Rows[i].Cells[4].Value.ToString(),out d1);
      

  3.   

    总结:
    先dataGridView1.Rows[i].Cells[n].Value判断是否为空
    然后数据类型转换
    然后再比较
      

  4.   


    你的事件应该是写在Validated 或Validating 中的吧,应该先用e.fomattedvalue.tostring.trim判断单元格是否为空(包含null和空格),当不为null或空的时候再用你的代码进行比较.因为dgv单元格在没赋值的时候取出来的值是nothing(null)
      

  5.   

    第四列的数据时通过键盘输入的,然后与第三列比较,我是这样写的,但发现第4列还是不能实例化 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;
                            }
                        }
                    }            }
            }        private void btncheck_Click(object sender, EventArgs e)
            {
                 groushowen.Location = new System.Drawing.Point(groushowen.Location.X,groushowen.Location.Y);
                 groushowen.Visible = true;
                 string s = null;
                 for (int i = 0; i < dataGridView1.Rows.Count; i++)
                 {
                     s = dataGridView1.Rows[i].Cells[4].Value.ToString();
                 }
                 this.enterListTableAdapter.Fill(this.wgycDataSet3.EnterList, s);
            }
      

  6.   

     
       第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;
                            }
                        }
                    }            }
            }        private void btncheck_Click(object sender, EventArgs e)
            {
                 groushowen.Location = new System.Drawing.Point(groushowen.Location.X,groushowen.Location.Y);
                 groushowen.Visible = true;
                 string s = null;
                 for (int i = 0; i < dataGridView1.Rows.Count; i++)
                 {
                     s = dataGridView1.Rows[i].Cells[4].Value.ToString();
                 }
                 this.enterListTableAdapter.Fill(this.wgycDataSet3.EnterList, s);
            }
      

  7.   

     
       第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;
                            }
                        }
                    }            }
            }        private void btncheck_Click(object sender, EventArgs e)
            {
                 groushowen.Location = new System.Drawing.Point(groushowen.Location.X,groushowen.Location.Y);
                 groushowen.Visible = true;
                 string s = null;
                 for (int i = 0; i < dataGridView1.Rows.Count; i++)
                 {
                     s = dataGridView1.Rows[i].Cells[4].Value.ToString();
                 }
                 this.enterListTableAdapter.Fill(this.wgycDataSet3.EnterList, s);
            }
      

  8.   

       第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;
                            }
                        }
                    }            }
            }        private void btncheck_Click(object sender, EventArgs e)
            {
                 groushowen.Location = new System.Drawing.Point(groushowen.Location.X,groushowen.Location.Y);
                 groushowen.Visible = true;
                 string s = null;
                 for (int i = 0; i < dataGridView1.Rows.Count; i++)
                 {
                     s = dataGridView1.Rows[i].Cells[4].Value.ToString();
                 }
                 this.enterListTableAdapter.Fill(this.wgycDataSet3.EnterList, s);
            }