Help with changing columns back color of DataGridView

Hi,

How do you change the backcolor of columns Headers if all the rows do not have identical data using VB.NET?

Thanks
Vicyor
vcharlesAsked:
Who is Participating?
 
Éric MoreauSenior .Net ConsultantCommented:
you can use a bit of Linq to find out columns with different values:
		For intCol As Integer = 0 To DataGridView1.Columns.Count - 1
			Dim strColName As String = DataGridView1.Columns(intCol).Name
			Dim values = From row In mdt.AsEnumerable() Select row.Field(Of Object)(strColName) Distinct
			If values.Count() > 1 Then
				DataGridView1.Columns(intCol).HeaderCell.Style.BackColor = Color.Red
			End If
		Next

Open in new window

0
 
Éric MoreauSenior .Net ConsultantCommented:
You need to set to False before changing the colors (otherwise it has no effect):

		DataGridView1.EnableHeadersVisualStyles = False
		DataGridView1.Columns(1).HeaderCell.Style.BackColor = Color.Red

Open in new window

0
 
vcharlesAuthor Commented:
Hi,

How do you declare mdt?

Thanks,

Victor
0
 
Éric MoreauSenior .Net ConsultantCommented:
at the top of my class
Dim mdt as DataTable

Open in new window

0
 
vcharlesAuthor Commented:
Thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.