vb.net 检查 datagridview 的每一行并获取列的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37196896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check each row of datagridview and get value of column(s)
提问by nora
I have a datagridview that is populated with a couple of rows. I not sure where to start to loop the grid from row 1 to the last row and check some values on two of the columns. My grid looks like this, I need to check the values of r1:c1 and r1:c4 using a loop. my c1 is checkbox column by the way.
我有一个填充了几行的数据网格视图。我不确定从哪里开始将网格从第 1 行循环到最后一行并检查其中两列的一些值。我的网格看起来像这样,我需要使用循环检查 r1:c1 和 r1:c4 的值。顺便说一下,我的 c1 是复选框列。
c1 c2 c3 c4 c5
r1 true 4 5 5 5
r2 false 4 5 5 5
r3 false 8 6 4 5
r4 false 5 1 2 3
r5 false 4 1 2 1
I've tried:
我试过了:
For Each row As DataGridViewRow In datagrid.Rows
If row.Cells("column1").Value = True Then
Dim getValue As String
getValue = row.Cells("column4").Value
msgbox(getValue)
End If
Next
Problm is, if any cell in column 1 is true, getValue is still getting set the value in column4. I only want the column4 value if column 1 row is true.
问题是,如果第 1 列中的任何单元格为真,则 getValue 仍在设置第 4 列中的值。如果第 1 列的行为真,我只想要 column4 值。
回答by nora
EDIT:
编辑:
This is what I'm using for the same purpose as you, but i'm checking for Strings/Integers so not sure how it would translate to boolean, and it works for returning a value of column 4-row(i)when you check the value of column 1-row(i). Hope this helps at least.
这就是我使用的与您相同的目的,但我正在检查字符串/整数,所以不确定它会如何转换为布尔值,并且value of column 4-row(i)当您检查value of column 1-row(i). 希望这至少有帮助。
For i = 0 To YourDataGridHere.RowCount - 1
If Not (YourDataGridHere.Rows(i).Cells("Column1").Value = 0) Then
MsgBox(YourDataGridHere.Rows(i).Cells("Column4").Value)
End If
Next

