繁体   English   中英

如何从datagridview获取单元格列值

[英]How to get cell column value from datagridview

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

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

我想获取列名,该怎么做? 谁能提供一些示例编码?

这是我的代码:

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

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

这将非常适合您。

将此方法放在您想要的任何位置的类中,然后调用它。 例如:按钮点击事件...

 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

PS这是我的测试屏幕截图。

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM