[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 666
  • Last Modified:

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

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
vcharles
Asked:
vcharles
1 Solution
 
vcharlesAuthor Commented:
Hi again,

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

Victor
0
 
Pratima PharandeCommented:
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
 
Jacques Bourgeois (James Burger)Commented:
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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
vcharlesAuthor Commented:
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
 
CodeCruiserCommented:
Change line 6 to

DataGridView1.Columns(Col).HeaderCell.Style.BackColor = Color.Red
0
 
Jacques Bourgeois (James Burger)Commented:
Sorry for the bad line. You just asked how to "change all columnheaders ", so I gave you the code for ALL column heaners.
0
 
vcharlesAuthor Commented:
Thank You, will get back to you in a few minutes.
0
 
vcharlesAuthor Commented:
It works.

Thank You.

Victor
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now