Solved

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

Posted on 2014-03-04
8
655 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

729 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