VB.net Datagridview cellstyle colors

S

Steve

Hi All

I am using VB.net 2005 professional

I set the background color of a datagridview cell to different colors
depending on many reasons

I set them in the cellformatting event and all works great, i.e the colors
appear as requested
e.g e.CellStyle.BackColor = Color.Red

When I try to read the color in Datagridview.mouseclick event using...
If
dgvgroup.Rows(hit.RowIndex).Cells(hit.ColumnIndex).InheritedStyle.BackColor
= mycolor Then

it always returns false

end if

Note: mycolor = color.salmonpink and actual grid cell color =
color.salmonpink

but
dgvgroup.Rows(hit.RowIndex).Cells(hit.ColumnIndex).InheritedStyle.BackColor
returns color.honeydew which is the defaultcellstyle.backcolor set in

datagridview properties defaultcellstyle in design environment

what am I doing wrong

Regards

Steve
 
L

Linda Liu [MSFT]

Hi Steve,

When you set the DataGridViewCellFormattingEventArgs.CellStyle.BackColor in
the CellFormatting event handler, you don't change the corresponding cell's
Style.

The DataGridViewCell.InheritedStyle property inherits its value from a
hierarchy of properties of type DataGridViewCellStyle. These properties are
listed below in the order in which the InheritedStyle for non-header cells
obtains its values.

1. System.Windows.Forms.DataGridViewCell.Style
2. System.Windows.Forms.DataGridViewRow.DefaultCellStyle
3. System.Windows.Forms.DataGridView.AlternatingRowsDefaultCellStyle (only
for cells in rows with odd index numbers)
4. System.Windows.Forms.DataGridView.RowsDefaultCellStyle
5. System.Windows.Forms.DataGridViewColumn.DefaultCellStyle
6. System.Windows.Forms.DataGridView.DefaultCellStyle

That's why you get Color.Honeydew from the
'dgvgroup.Rows(hit.RowIndex).Cells(hit.ColumnIndex).InheritedStyle.BackColor
' because the DefaultCellStyle.BackColor is set to Color.Honeydew in your
practice.

If you modify your code as follows, you can get the actual color of the
cell through the InheritedStyle.BackColor property:

Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e
As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles
DataGridView1.CellFormatting
If (e.RowIndex = 0) Then

Me.DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.BackColor =
Color.Red
End If
End Sub

For more information on cell styles in the DataGridView control, you may
refer to the following MSDN document:
'Cell Styles in the Windows Forms DataGridView Control'
http://msdn2.microsoft.com/en-us/library/1yef90x0(VS.80).aspx

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
L

Linda Liu [MSFT]

Hi Steve,

How about the problem now?

If you have any question, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Sevice!

Sincerely,
Linda Liu
Microsoft Online Community Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top