vb.net 2008 - how to highlight a specific row in a datagridview

I'm looping through the rows in my datagridview and want to highlight one when I find a certain match.  How do I do that last part?  See commented line below.

       Dim myrow As DataGridViewRow
        For x = 0 To Me.DataGridView1.Rows.Count
            myrow = DataGridView1.Rows(x)
            If myrow.Cells(0).Value.ToString.Trim = MatchingKeyCodeThen
                ' ?? Can't find command to highlight row(x).
                Exit For
            End If
        Next

TIA
jackiemeckAsked:
Who is Participating?
 
nepaluzConnect With a Mentor Commented:
try the currentcell

Me.DataGridView1.CurrentCell=MatchingKeyCode
0
 
Paul JacksonSoftware EngineerCommented:
use myrow.BackColor and myrow.ForeColor to change the colors so its highlighted in the way you want

myRow.BackColor = Drawing.Color.Crimson
myRow.ForeColor = Drawing.Color.White
0
 
jackiemeckAuthor Commented:
I'm sorry...I used an unclear term.   I have the datagridview property set (DataGridViewSelectionMode.FullRowSelect)  so that when one cell is clicked, the entire row is highlighted/selected.  I want to be able to manually select the current record when the user comes back to this screen, meaning, I need to simulate that the user clicked a specific row.  I don't believe the backcolor will mark this row "selected".
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Paul JacksonSoftware EngineerCommented:
try : myRow.ApplyStyle(DataGridView1.SelectedRowStyle)
0
 
jackiemeckAuthor Commented:
myrow.applystyle isn't pulling up as a supported function.  Neither does "selectedRowStyle" for DataGridView.  This is VB.net 2008...is that a new function?
0
 
Paul JacksonSoftware EngineerCommented:
Hmm is this windows forms it is isn't it.
0
 
jackiemeckAuthor Commented:
Yes...Now I'm finding there are many differences between DataGrid and DataGridView...I'm using the latter.
0
 
Paul JacksonSoftware EngineerCommented:
what about :

myrow.BackColor = SystemColors.Highlight
myrow.ForeColor = SystemColors.HighlightText
0
 
jackiemeckAuthor Commented:
I found it thanks to nepaluz...the following works, where 0 is the column position and x is the desired row:

 Me.DataGridView1.CurrentCell = Me.DataGridView1.Item(0, x)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.