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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

É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
É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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.