简体   繁体   English

如何从datagridview获取单元格列值

[英]How to get cell column value from datagridview

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? 谁能提供一些示例编码?

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,我想更改backgroundcolor。

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

PS Here's my screenshot of my test. PS这是我的测试屏幕截图。

在此处输入图片说明

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

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