Victor Charles
asked on
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
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
try this , will change header style for all columns.
Protected Sub GridView1_RowDataBound(sen der 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.Fore Color = Color.OrangeRed
End If
End If
7 End Sub
Protected Sub GridView1_RowDataBound(sen
If e.Row.RowType = DataControlRowType.DataRow
If e.Row.Cells(2).Text ='BEL' OR e.Row.Cells(2).Text =''USA'' Then
GridView1.HeaderStyle.Fore
End If
End If
7 End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
Thanks,
Victor
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
Thanks,
Victor
Change line 6 to
DataGridView1.Columns(Col) .HeaderCel l.Style.Ba ckColor = Color.Red
DataGridView1.Columns(Col)
Sorry for the bad line. You just asked how to "change all columnheaders ", so I gave you the code for ALL column heaners.
ASKER
Thank You, will get back to you in a few minutes.
ASKER
It works.
Thank You.
Victor
Thank You.
Victor
ASKER
Is it also possible to set the condition for multiple values (i.e.Textbox.Text = 'BEL', 'USA')
Victor