我在根据下一行的值设置单元格值时遇到了这个问题。
例如;
Remarks Reference
A 50
A 25
B 5
-25案例1:
如果“引用”列中下一个Cell.Row的值是正数,那么“备注”列中的cell.row值是"A“。
案例2:
如果“引用”列中下一个Cell.Row的值为负数,那么“备注”列中的cell.row值为"B“。
我已经尝试过这段代码了,但是我无法得到我需要的结果。
Dim Bal As Decimal = 0
For Each r As DataGridViewRow In dgvSTSub.Rows
Bal = Bal + r.Cells(3).Value - r.Cells(1).Value
r.Cells(3).Value = Bal
If Bal > 0 Then
r.Cells(2).Value = Bal
ElseIf Bal < 0 Then
r.Cells(2).Value = r.Cells(3).Value
End If
Next请跟我分享一下如何做这件事的密码。
发布于 2017-10-09 09:48:28
首先,我建议使用For循环而不是ForEach,因为遍历行变得更加方便和容易。
其次,在访问ColumnNames时使用Index代替Index,例如:使用.Cells("reference")(引用为列名)代替.Cells(1)。我之所以建议这样做,是因为如果将来您碰巧重新排序您的方法和函数的列,除非您更改了列的名称,否则不会受到阻碍。
注意:这里的ColumnName不是您在DataGridView UI中看到的标题文本。(希望你已经知道了)
由于您还没有显示您设置的备注位置,这应该会让您了解如何实现它:
For index = 0 To DataGridView1.RowCount - 1
Dim nxt = 1
If (index + 1 < DataGridView1.RowCount) Then
nxt = DataGridView1.Rows(index + 1).Cells("reference").Value
End If
If (nxt) > 0 Then
DataGridView1.Rows(index).Cells("remark").Value = "A"
Else
DataGridView1.Rows(index).Cells("remark").Value = "B"
End If
Next虚拟输出:
Remark Reference
A 13
B 21
A -33
B 41
A -54https://stackoverflow.com/questions/46642497
复制相似问题