将文本框与单元格相乘 [英] Multiply textbox with cell
本文介绍了将文本框与单元格相乘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在datagridview中将文本框与单元格相乘,我尝试将其细分为几段,有人知道我出了错吗?
I am trying to multiply a textbox with a cell in the datagridview, I''ve tried breaking it down into a few segments, does anyone know where I''ve gone wrong?
public int a, b, c, i;
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
foreach (DataGridViewRow dg in dataGridView1.Rows)
{
try
{
a = int.Parse(OdrQuantityTxtBx.Text);
b = (int)dataGridView1.Rows[i].Cells[1].Value;
c = (a * b);
c = dataGridView1.Columns[i].Cells[1].Value;
}
catch
{
}
}
}
我尝试过的事情:
对a,b,c使用``public double''
What I have tried:
using ''public double'' for a,b,c
推荐答案
首先,永远不要吞咽"异常-这隐藏了问题,因为您甚至没有得到告诉我这是一个问题.
其次,不要在正常处理"中使用异常-代替try ... catch块来处理转换失败,请使用TryParse,它会根据转换值的成功或失败返回true或false. /> 第三,如果最终结果始终只是最后一行的值,为什么还要使用循环呢?
First off, never "swallow" exceptions - that hides problems because you don''t even get told it''s a problem.
Second, don''t use exceptions as part of "normal processing" - instead of a try...catch block to cope with conversion failures, use TryParse which returns true or false depending on success or failure to convert the value.
Third, why are you using a loop at all, if your final result will alway be the value from the last row only?
1)放弃try-catch-block.它可以检测仅精确到该块中某处"的错误,您可能需要更高的准确性.仅仅将异常吞入一个空的catch块并不能解决任何问题.它只是隐藏了真正的问题,并阻止您修复它们.
2)int.TryParse(OdrQuantityTxtBx.Txt, out a)
包含try-catch.它返回一个布尔值,您可以测试是否成功解析.
3)dataGridView1.Rows[i].Cells[1].Value
是一个字符串.您不能仅将其强制转换为int
.您需要再次合并2).
4)c = dataGridView1.Columns[i].Cells[1].Value;
用单元格值覆盖您从一行开始的宝贵乘法结果.我想你想反过来.
但是该单元格与您最初用于乘法的单元格相同.您可能需要将结果存储在另一个单元格中.而且.Columns [i]没有名为"Cells"的成员.
而且我仍然假设.Value为字符串类型,因此您不能将其分配给整数或双精度数.
因此,也许有另一列并使用
dataGridView1.Rows[i].Cells[2].Value = c.ToString();
1) Ditch the try-catch-block. It can detect errors only accurate to "somewhere in that block", you may need more accuracy. And just swallowing the exception in an empty catch block doesn''t solve anything. It just hides the real problems and prevents you from fixing them.
2)int.TryParse(OdrQuantityTxtBx.Txt, out a)
has a try-catch included. It returns a boolean you can test for successful parsing.
3)dataGridView1.Rows[i].Cells[1].Value
is a string. You cannot just cast it toint
. You need to incorporate 2) again.
4)c = dataGridView1.Columns[i].Cells[1].Value;
overwrites your precious multiplication result from one line earlier with a cell value. I think you want it to be the other way round.
But that cell is the same you originally used for the multiplication. You may want to store the result in another cell instead. And .Columns[i] doesn''t have a member named "Cells".
And I''m still assuming .Value to be of type string, so you can''t assign it to an integer, nor a double.
So maybe have another column and use
dataGridView1.Rows[i].Cells[2].Value = c.ToString();
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
int a;
int b;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (int.TryParse(OdrQuantityTxtBx.Text, out a) &&
int.TryParse(row.Cells[1].Value.ToString(), out b))
{
// your code was overwriting the value that was in one of the
// cells you were using for the math. That seemed peculiar to
// me.
row.Cells[2].Value = a * b;
}
else
{
// handle the error by either throwing an exception or displaying an
// error indicator in the cell that contains the result.
}
}
}
这篇关于将文本框与单元格相乘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文