Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1112
  • Last Modified:

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
0
jackiemeck
Asked:
jackiemeck
  • 4
  • 4
1 Solution
 
Paul JacksonCommented:
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
 
Paul JacksonCommented:
try : myRow.ApplyStyle(DataGridView1.SelectedRowStyle)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
nepaluzCommented:
try the currentcell

Me.DataGridView1.CurrentCell=MatchingKeyCode
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 JacksonCommented:
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 JacksonCommented:
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now