vb.net 如何从datagridview获取单元格列值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23380501/
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
How to get cell column value from datagridview
提问by RomJoy
How to get cell column value from datagridview and if it is equal to OVERDUE, the back cell color will change.
如何从datagridview获取单元格列值,如果等于OVERDUE,后面的单元格颜色会改变。
----------------------------------
id | uname | lname |
---------------------------------
1 | anne| | a |
----------------------------------
2 | rays | b |
----------------------------------
3 | saws | OVERDUE | <---------- this row will become yellow This row only.
----------------------------------
I want to get column lname ,how to do that? Can anyone give some example coding?
我想得到列 lname ,怎么做?谁能给出一些示例编码?
This is my code:
这是我的代码:
If gridalarmnotice(20, gridalarmnotice.CurrentCell.RowIndex).Value.ToString = "OVERDUE" Then
gridalarmnotice.DefaultCellStyle.BackColor = Color.Yellow
End If
I want to change backgroundcolor if the column and current cell is equal to OVERDUE.
如果列和当前单元格等于 OVERDUE,我想更改背景颜色。
采纳答案by ???ěxě?
This will work just great for you.
这对你很有用。
Put this method in your class wherever you want... Then call it. . For ex: a button click event...
把这个方法放在你的类中你想要的任何地方......然后调用它。. 例如:按钮单击事件...
Private Sub HighlightRows()
'Loop through the rows, if the current rows cell equals "OVERDUE"
'change the rows color to yellow
For i As Integer = 0 To gridalarmnotice.Rows.Count - 1
If gridalarmnotice.Rows(i).Cells("lname").Value.Equals("OVERDUE") Then
'Then color the whole row, each cell specifically
For Each cell As DataGridViewCell In gridalarmnotice.Rows(i).Cells
cell.Style.BackColor = Color.Yellow
Next
End If
Next
End Sub
P.S. Here's my screenshot of my test.
PS 这是我的测试截图。



