如何将datagridview列值与文本框值相乘。 [英] How do I multiply datagridview column values with a text box value.
本文介绍了如何将datagridview列值与文本框值相乘。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我生成一些行然后我输入文本框值.............,但是首先如果我输入文本框值然后生成行,则代码工作正常。它不执行操作,总值显示在totPenTxt.Text为零。
我尝试过:
The Code is working fine if i generate some rows and then i input Text box Value ............. but first if i input Text box value and then generate rows. It do not perform action and the total value shows in totPenTxt.Text zero.
What I have tried:
private void penTxt_TextChanged(object sender, EventArgs e)
{
int sum = 0;
for (int i = 0; i < metroGrid1.Rows.Count - 1; ++i)
{
sum += Convert.ToInt32(metroGrid1.Rows[i].Cells["quantity"].Value);
}
if (penTxt.Text == string.Empty && totPenTxt.Text != string.Empty)
{
totPenTxt.Text = Convert.ToInt32(0).ToString();
}
else
{
int value = Convert.ToInt32(penTxt.Text);
int result = value * sum;
totPenTxt.Text = result.ToString();
}
推荐答案
如果GridView中没有行,则sum将为零。你需要添加一个正确的行数检查,如果没有,则采取不同的行动。
另外......
If there are no rows in the GridView then sum will be zero. You need to add a proper check for the number of rows and take a different action if there are none.
Also ...
totPenTxt.Text = Convert.ToInt32(0).ToString();
// why not just:
totPenTxt.Text = "0";
这篇关于如何将datagridview列值与文本框值相乘。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文