Solved

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

Posted on 2011-02-24
9
992 Views
Last Modified: 2012-05-11
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
Comment
Question by:jackiemeck
  • 4
  • 4
9 Comments
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 34975080
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
 

Author Comment

by:jackiemeck
ID: 34975103
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
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 34975231
try : myRow.ApplyStyle(DataGridView1.SelectedRowStyle)
0
 
LVL 17

Accepted Solution

by:
nepaluz earned 500 total points
ID: 34975255
try the currentcell

Me.DataGridView1.CurrentCell=MatchingKeyCode
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:jackiemeck
ID: 34975271
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
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 34975284
Hmm is this windows forms it is isn't it.
0
 

Author Comment

by:jackiemeck
ID: 34975301
Yes...Now I'm finding there are many differences between DataGrid and DataGridView...I'm using the latter.
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 34975320
what about :

myrow.BackColor = SystemColors.Highlight
myrow.ForeColor = SystemColors.HighlightText
0
 

Author Comment

by:jackiemeck
ID: 34975364
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now