求教关于Datagridview 的 CellValueChanged 事件问题

xchjian012 2012-03-22 11:41:14
想实现 手动输入第3列与第5列的数值,第六列自动求出两列数值的乘积,代码如下,运行提示未处理的“System.StackOverflowException”类型的异常,求教原因和改正方法.....

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{

int i = e.RowIndex;
if (i >= 0)
{
dataGridView1.Rows[e.RowIndex].Cells[6].Value = Math.Round((Convert.ToDecimal(dataGridView1.Rows[e.RowIndex].Cells[3].Value) * Convert.ToDecimal(dataGridView1.Rows[e.RowIndex].Cells[5].Value)));
}
}
...全文
232 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
xchjian012 2012-03-22
  • 打赏
  • 举报
回复
终于解决,跪谢各位,代码如下:
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
int i = e.RowIndex;
dataGridView1.CellValueChanged -= new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);
try
{

if (i >= 0)
{
decimal value1 = Convert.ToDecimal(dataGridView1.Rows[e.RowIndex].Cells[2].Value);
decimal value2 = Convert.ToDecimal(dataGridView1.Rows[e.RowIndex].Cells[3].Value);
dataGridView1.Rows[e.RowIndex].Cells[5].Value = Math.Round(value1 * value2);
}
}
finally
{
dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);
}


}
  • 打赏
  • 举报
回复
我之前遇到过报错,具体记不清楚,你尝试一下这样:

decimal value1 = Convert.ToDecimal(dataGridView1.Rows[e.RowIndex].Cells[4].Value);
decimal value2 = Convert.ToDecimal(dataGridView1.Rows[e.RowIndex].Cells[5].Value);
dataGridView1.Rows[e.RowIndex].Cells[7].Value = Math.Round(value1 * value2);
xchjian012 2012-03-22
  • 打赏
  • 举报
回复
还是同样的错误,应该计算方法的问题
  • 打赏
  • 举报
回复
你写到CellendEdit事件吧...

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
int i = e.RowIndex;
try
{
if (i >= 0)
{
dataGridView1.Rows[e.RowIndex].Cells[7].Value
= Math.Round((Convert.ToDecimal(dataGridView1.Rows[e.RowIndex].Cells[4].Value) *
Convert.ToDecimal(dataGridView1.Rows[e.RowIndex].Cells[5].Value)));
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
xchjian012 2012-03-22
  • 打赏
  • 举报
回复
试了各种方法,都是同一种错误....
  • 打赏
  • 举报
回复
你试试这段代码...不知道你哪里觉得错了?
int i = e.RowIndex;
try
{
if (i >= 0)
{
dataGridView1.Rows[e.RowIndex].Cells[7].Value
= Math.Round((Convert.ToDecimal(dataGridView1.Rows[e.RowIndex].Cells[4].Value) *
Convert.ToDecimal(dataGridView1.Rows[e.RowIndex].Cells[5].Value)));
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
xchjian012 2012-03-22
  • 打赏
  • 举报
回复
不好意思,小弟初学,finally是套用网上一个相似案例的,貌似不好使,请教该如何种植循环呢?
EnForGrass 2012-03-22
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 xchjian012 的回复:]

我做了个这个,可还是有这个提示,似乎还是死循环的原因,麻烦帮忙看看哪里有问题~~
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{

int i = e.RowIndex;
try
……
[/Quote]
你finally里那句干嘛?
xchjian012 2012-03-22
  • 打赏
  • 举报
回复
我做了个这个,可还是有这个提示,似乎还是死循环的原因,麻烦帮忙看看哪里有问题~~
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{

int i = e.RowIndex;
try
{

if (i >= 0)
{
dataGridView1.Rows[e.RowIndex].Cells[7].Value = Math.Round((Convert.ToDecimal(dataGridView1.Rows[e.RowIndex].Cells[4].Value) * Convert.ToDecimal(dataGridView1.Rows[e.RowIndex].Cells[5].Value)));
}
}
finally
{
dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
}


}
bdmh 2012-03-22
  • 打赏
  • 举报
回复
应该是你对Cells[6].Value赋值,有引发了CellValueChanged事件,导致死循环,你还是做个标记退出这个死循环吧
xchjian012 2012-03-22
  • 打赏
  • 举报
回复
不是列号的问题,第0-7列都是数字。
bdmh 2012-03-22
  • 打赏
  • 举报
回复
Cells[6].Value,这是第7列,下标从0开始
xchjian012 2012-03-22
  • 打赏
  • 举报
回复
没人看么,自己顶下

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧