Solved

detect background color in datagridview cell

Posted on 2010-11-22
5
726 Views
Last Modified: 2012-05-10
I have a datagrid that has different colors on different rows based on criteria found in different sources.  When I click on that datagrid, a detail form opens up.  I want to detect the background color of the row that was clicked on and make my other form background the same color.

So how can I find out what the current background color is set to on a datagridview row?

Thanks!
0
Comment
Question by:cbrown-aps
  • 2
  • 2
5 Comments
 
LVL 13

Expert Comment

by:gamarrojgq
Comment Utility
Hi,

Try this

        If DataGridView1.SelectedRows.Count > 0 Then
            If DataGridView1.SelectedRows(0).Cells(0).Style.BackColor = Color.Aqua Then
                'Your Code Here
                '....
            End If
        End If
0
 
LVL 13

Expert Comment

by:iHadi
Comment Utility
Hi

Try the following:

If dataGridView1.SelectedCells.Count > 0 Then
    Dim index as integer = dataGridView1.SelectedCells(0).RowIndex
    Dim c as Color = dataGridView1.Rows(index).DefaultCellStyle.BackColor
    Me.BackColor = c ' for demo only
End If
0
 

Author Comment

by:cbrown-aps
Comment Utility
This is happening in my mouse down event.   Neither of these are working for me.
Maybe if you saw how I was getting a value to pass to the child it would spark an idea...

 client = DataGridView1.Item(0, rIndex).Value

Any ideas how I could get it from this approach?
0
 
LVL 13

Accepted Solution

by:
gamarrojgq earned 250 total points
Comment Utility
do you have a particulary reason to use the Mouse Down Event? because you can try using CellClick Event like this

    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick

        If DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.BackColor = Color.Aqua Then
            'Your Code Here
            '....
        End If

End Sub
0
 

Author Closing Comment

by:cbrown-aps
Comment Utility
The solution achieved my goal however using a different method.

Thanks for great work!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now