Solved

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

Posted on 2011-02-24
9
997 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 17

Accepted Solution

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

Me.DataGridView1.CurrentCell=MatchingKeyCode
0
 

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Crystal reports vb.net 2 41
VB.NET 2008 Publish Error 2 27
.NET 2008 VB and C# 6 28
MailAddress in vb 4 17
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

831 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