简体   繁体   中英

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.

----------------------------------
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.

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.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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