Solved

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

Posted on 2014-03-04
8
623 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
 

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

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

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

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

13 Experts available now in Live!

Get 1:1 Help Now