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谢谢!!
解决方案 »
- 初学C#,关于类和对象的概念不是很理解,求教。
- 关键词: *目标组件* 拖拽 *
- 求证一下,Webbrowser类只能在Form中使用吗?解释一下原因好吗
- enum 为什么只可以做-=计算?
- System.Timer.Timer先执行后等待
- c# winform 全角自动转化半角问题
- [讨论]数据导入模块的模型,思路等等,小弟没做过,请各位给点意见和建议!
- 弱弱的问 在vs.net中添加一个celender控件怎么显示一二三四五六日
- 接口开发,带string和struct参数方法编写,图片base64Binary编码编写(c#开发)
- 请教一下有关搜索引擎的问题
- Javascript代码出错,如何解决?
- 跪求c#提取图片轮廓方法
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尚没有数据.
double.TryParse(dataGridView1.Rows[i].Cells[4].Value.ToString(),out d1);
先dataGridView1.Rows[i].Cells[n].Value判断是否为空
然后数据类型转换
然后再比较
你的事件应该是写在Validated 或Validating 中的吧,应该先用e.fomattedvalue.tostring.trim判断单元格是否为空(包含null和空格),当不为null或空的时候再用你的代码进行比较.因为dgv单元格在没赋值的时候取出来的值是nothing(null)
{
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);
}
第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);
}
第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);
}
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);
}