函数在 gridview , vb.net 中获取特定的单元格值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16954314/
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
Function get specific cell value in gridview , vb.net
提问by shaik ibrahim
What i want to do is get a value from the data grid. Example: a button onclick will change the color of the cell if the cell value is "test". i have the coding on how to change color but how to do the loop to search the value. Considering that only 1 column has a cell with the value test.
我想要做的是从数据网格中获取一个值。示例:如果单元格值为“test”,则单击按钮将更改单元格的颜色。我有关于如何改变颜色但如何进行循环来搜索值的编码。考虑到只有 1 列具有带有值测试的单元格。
回答by matzone
Try this ..
尝试这个 ..
For y As Integer = 0 To DataGridView1.Rows.Count - 1
For x As Integer = 0 to DataGridView1.Columns.Count - 1
If Datagridview.Rows(y).Cells(x).Value = "test" Then
DataGridView1..Rows(y).Cells(x).Style.ForeColor = Color.Red
End If
Next
Next
回答by Jacob Siemaszko
You need to loop thru the rows and search for the text.
您需要遍历行并搜索文本。
in this one you find out how to loop
在这个你会发现如何循环
Visual Basic, How do I read each row in a datagrid?
additionally if you have multiple columns and want to loop thru them too then you have to nest this peace in another loop, something like
此外,如果您有多个列并且也想通过它们循环,那么您必须将这种和平嵌套在另一个循环中,例如
For i As Integer = 0 To DataGridView1.ColumnCount
For Each row As DataGridViewRow In DataGridView1.Rows
If Not row.IsNewRow Then
If row.Cells(i).Value.ToString = "test" Then DataGridView1.Item(i, row.Index).Style.ForeColor = Color.Red
End If
Next
Next i
Good luck
祝你好运

