Solved

Help with changing headertext color of DataGridView if value found using VB.NET

Posted on 2014-03-04
8
649 Views
Last Modified: 2014-03-05
Hi,

How do you loop through a DataGrid View and change all columnheaders text color if a value is found (i.e. textboxt.text = BEL) in their cell?

Thanks,

Victor
0
Comment
Question by:vcharles
8 Comments
 

Author Comment

by:vcharles
ID: 39905546
Hi again,

Is it also possible to set the condition for multiple values (i.e.Textbox.Text = 'BEL', 'USA')

Victor
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 39905574
try this , will change header style for all columns.

Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs)  

   If e.Row.RowType = DataControlRowType.DataRow Then

         If e.Row.Cells(2).Text ='BEL' OR e.Row.Cells(2).Text =''USA''   Then

         GridView1.HeaderStyle.ForeColor = Color.OrangeRed

        End If

     End If

7 End Sub
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 39905664
Pratima code is for a GridView, in a web application.

In a DataGridView, the following will do the trick:

	Sub CheckForValues()

		For row As Integer = 0 To DataGridView1.Rows.Count - 1
			For col As Integer = 0 To DataGridView1.Columns.Count - 1
				If CStr(DataGridView1.Item(col, row).Value) = "BEL" Or CStr(DataGridView1.Item(col, row).Value) = "USA" Then
					DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Red
					Exit Sub
				End If
			Next
		Next

	End Sub

Open in new window

0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

Author Comment

by:vcharles
ID: 39906266
Hi,

I tried the code below, but all the colomn hearders are affected, how do I only change the colors of the columns which contains the values?

Sub CheckForValues()

		For row As Integer = 0 To DataGridView1.Rows.Count - 1
			For col As Integer = 0 To DataGridView1.Columns.Count - 1
				If CStr(DataGridView1.Item(col, row).Value) = "BEL" Or CStr(DataGridView1.Item(col, row).Value) = "USA" Then
					DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Red
					Exit Sub
				End If
			Next
		Next

	End Sub

Open in new window


Thanks,

Victor
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39906270
Change line 6 to

DataGridView1.Columns(Col).HeaderCell.Style.BackColor = Color.Red
0
 
LVL 40
ID: 39906911
Sorry for the bad line. You just asked how to "change all columnheaders ", so I gave you the code for ALL column heaners.
0
 

Author Comment

by:vcharles
ID: 39906930
Thank You, will get back to you in a few minutes.
0
 

Author Comment

by:vcharles
ID: 39906972
It works.

Thank You.

Victor
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

697 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