Click here to Skip to main content
15,881,653 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted

Hi

I Think This code is helpful you
VB
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);
 
Share this answer
 
v3
Comments
VJ Reddy 7-Jun-12 2:33am    
Good suggestion. 5!
Member 10030245 21-May-13 15:26pm    
on which event
The reason for getting the out of range error may be because the DataGridView Rows collection is zero based whereas in the following code
VB
c = (DataGridView1.Item(j + 3, r + 1).value * DataGridView1.Item(j + 2, r + 1).value)

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:
VB
c = Convert.ToDouble(DataGridView1.Item(j + 3, r + 1).value) * Convert.ToDouble(DataGridView1.Item(j + 2, r + 1).value)
 
Share this answer
 
Comments
Syed Hamza 7-Dec-19 4:07am    
how can i do this in datagrid.
thanks in advance

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900