如何从datagridview获取单元格列值

时间:2014-04-30 05:43:04

标签: vb.net

如何从datagridview获取单元格列值,如果它等于OVERDUE,则后单元格颜色将会改变。

----------------------------------
id     |   uname    |  lname     |
---------------------------------
1      |   anne|    |    a       |
----------------------------------
2      |   rays     |    b       |
----------------------------------
3      |   saws     |  OVERDUE   |     <---------- this row will become yellow This row only.
----------------------------------

我想获得列lname,该怎么做?任何人都可以提供一些示例编码吗?

这是我的代码:

  If gridalarmnotice(20, gridalarmnotice.CurrentCell.RowIndex).Value.ToString = "OVERDUE" Then
      gridalarmnotice.DefaultCellStyle.BackColor = Color.Yellow  
  End If

如果列和当前单元格等于OVERDUE,我想更改backgroundcolor。

1 个答案:

答案 0 :(得分:0)

这对你很有帮助。

将此方法放在您班级的任何地方......然后调用它。 。例如:按钮点击事件......

 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。这是我测试的截图。

enter image description here