问题描述
你好,
我希望将两列的值一一乘以两,然后在文本框中打印这些相乘的答案的总和.我已经写了一个代码,但是它给出了超出范围的错误.代码是这样的:
d = 0
对于r = 0到a-1
c =(DataGridView1.Item(j + 3,r + 1).value * DataGridView1.Item(j + 2,r + 1).value)
d = d + c
下一个r
TextBox2.Text = Str(d)
这里"r"是行,"a"是用户给出的行总数."j"是列
请尽快尽快答复我.
hello,
i want two multiply two column''s value one by one and then print the sum of these multiplied answers in a text box. i have written a code but it gives out of range error.the code is this:
d = 0
For r = 0 To a - 1
c = (DataGridView1.Item(j + 3, r + 1).value * DataGridView1.Item(j + 2, r + 1).value)
d = d + c
Next r
TextBox2.Text = Str(d)
here "r" are rows and "a" is total no.of rows given by user."j" are columns
kindly reply me as soon as possible i need this urgently.
推荐答案
Double val1 = Convert.ToDouble(dataGridView1.Rows[e.RowIndex].Cells[4].Value);
Double val2 = Convert.ToDouble(dataGridView1.Rows[e.RowIndex].Cells[6].Value);
dataGridView1.Rows[e.RowIndex].Cells[7].Value = val1 * val2;
//val3 = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[8].Value);
//tb_grossamt.Text = (val1 * val2).ToString();
Double valgrd = (val1 * val2);
Double valtax = Convert.ToDouble(tb_tax.Text);
Double taxtot = (valgrd * valtax / 100);
Double ftot = (valgrd + taxtot);
c = (DataGridView1.Item(j + 3, r + 1).value * DataGridView1.Item(j + 2, r + 1).value)
>
循环的upper bound value
的r+1
使Row index
成为a
,这会导致上述错误.
因此,在上面的代码中用r
替换r+1
,最好将值转换为Double
以便进行乘法运算,如Solution 1
中已经建议的那样,如下所示:
r+1
for upper bound value
of the loop makes Row index
as a
which causes the above error.
So replace r+1
with r
in the above code further it is better to convert the value to Double
for multiplication as already suggested in Solution 1
, as shown below:
c = Convert.ToDouble(DataGridView1.Item(j + 3, r + 1).value) * Convert.ToDouble(DataGridView1.Item(j + 2, r + 1).value)
这篇关于datagridview列相乘,然后将它们相加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!