如何循环来计算datagridview中的单元格值? [英] How to do loop to calculate the cell values in datagridview?

查看:80
本文介绍了如何循环来计算datagridview中的单元格值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好......



如何将一行列的值乘以另一列中的所有值,并为所有行执行循环。然后,最后,将每行的值相加?



循环仅适用于第3列中的前两行.. !!?我想不明白!非常感谢你的帮助!



这是一个例子。

请注意,我不需要拥有或显示这三列(I,II,III)。我只是把它们放在那里展示步骤。第2列可能有很多行...









Hello ...

How to multiply a value of a row of a column to all the values in another column and do the loop for all rows.Then, at the end, sum the values in each row?

The loop works only for the 1st two rows in Column 3..!!? I can not figure it out! Thanks a lot for your help!

Here is an example.
Please note that I do not need to have or show these three columns (I, II, III). I just put them in there to show the steps. Column 2 could have lots of rows...




Column 1	Column 2	I	II	III	   Column 3 (I+II+III)
  40	           1    40			      40
  30	           2	30	80		      110
  20	           5	20	60	200	      280
  10		            10  40	150	      200
   5		            5	20	100	      125
		                    10   50	      60
		       	                 25	      25





我的尝试:





What I have tried:

Dim i As Integer
For k As Integer = 0 To Data1.ColumnCount - 4

For j = 0 to Data1.Rows.Count - 2

Data1.Rows(j).Cells(2).Value = Data1.Rows(j).Cells(0).Value * _
Data1.Rows(j).Cells(1).Value 


For i = 0 To EOF()

i = i + 1

Data1.Rows(j).Cells(3).Value = Data1.Rows(j).Cells(0).Value *_ 
Data1.Rows(k+i).Cells(1).Value + Data1.Rows(j).Cells(2).Value

Next i 
Next j
Next k

推荐答案

参见
For k As Integer = 0 To Data1.ColumnCount - 4

你忽略了最后3列。该行应该读取

You are ignoring the last 3 columns. That line should probably read

For k As Integer = 0 To Data1.ColumnCount - 1

或者你可以使用 For Each 来避免这个问题。

还要看行

Or you could use For Each to avoid the problem.
Also look at line

For j = 0 to Data1.Rows.Count - 2

你忽略了最后一排。该行应该读取

You are ignoring the last row. That line should probably read

For j = 0 to Data1.Rows.Count - 1

现在看看你的内部循环

For i = 0 To EOF()

i = i + 1

您在<$中递增计数器 i c $ c>对于循环,所以第一次通过该循环i = 1(不是0)。第二次通过循环i = 3不是1,第三次是5而不是2,依此类推。我真的不明白为什么你有三个循环来遍历一个二维物体我不知道EOF()函数是做什么的。



你也需要了解网格中的每列具有相同数量的行 - 网格具有行(和列),列不具有行。



你已经有了调试但没有注意到这些问题,所以希望这篇文章 - 掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ] - 将帮助您变得更加精通

You are incrementing the counter i within the For loop, so the first time through that loop i = 1 (not 0). The second time through the loop i = 3 not 1, third time 5 not 2 and so on. I can't really understand why you have three loops to traverse a two-dimensional object and I have no idea what the function EOF() is doing.

You also need to understand that each column in the grid has the same number of "rows" - Grids have Rows (and Columns), Columns do not.

You've already had a go at debugging but didn't notice these issues, so hopefully this article - Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^] - will help you become more proficient


这篇关于如何循环来计算datagridview中的单元格值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆