Solved

Changing datagridview row colors at runtime

Posted on 2007-11-28
18
5,435 Views
Last Modified: 2011-04-14
Hello experts,

VS2005/VB.Net

I am buidling a datagridview with data retreived from SQL queries. As data is gathered I am adding rows to the datagridview using:

                'Header row
                Dim row As String() = {"Useless information here"}
                DataGridView1.Rows.Add(row)

-or-

            ' Add row with multiple columns of data
            Dim row As String() = {"Here it is", Data1, Data2, Data3, Data4, Data5}
            DataGridView1.Rows.Add(row)

I would LOVE to be able to change the background color and or font on certain rows as they are added, for example make the header row blue so they stand out a little better.

Thanks,
Michael
0
Comment
Question by:webkiwi1
  • 9
  • 8
18 Comments
 
LVL 27

Expert Comment

by:VBRocks
ID: 20366061
You can change the background color of the column headers like this:

        Me.DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Blue
        Me.DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.White

0
 
LVL 27

Expert Comment

by:VBRocks
ID: 20366091
To setup alternating colors, you can do it like this:

        Me.DataGridView1.RowsDefaultCellStyle.BackColor = Color.White
        Me.DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Yellow



0
 

Author Comment

by:webkiwi1
ID: 20366254
Thanks for the suggestions.

I was not too clear in my request as far as when I referred to Header row, I did not mean the main header row of the datagridview...sorry.

As I build the datagridview, I am adding rows that define what follows, i.e. "Useless information", and then sveral rows of stuff, and then "Important information", and another few rows of stuff, all within the same datagridview.

It is those rows (Useless, important etc.) individually I would like to assign a different color and/or fotn style (eg. bold).
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 20366478
Ok, you can do that.

For your DataGridView, use the RowsAdded event.  Here's an example:

    Private Sub DataGridView1_RowsAdded(ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) _
        Handles DataGridView1.RowsAdded

        'Setup all of the styles you want here
        Dim style1 As New System.Windows.Forms.DataGridViewCellStyle
        style1.BackColor = Color.Blue
        style1.ForeColor = Color.White

        Dim style2 As New System.Windows.Forms.DataGridViewCellStyle
        style2.BackColor = Color.Yellow
        style2.ForeColor = Color.Blue
        'End of styles


        'Loop through all of the rows that were added,
        '  check the value of whatever column you want
        '  change the cellstyle for that row
        Dim row As DataGridViewRow

        'Index of first row added
        For idx As Int32 = e.RowIndex To e.RowCount - 1

            row = Me.DataGridView1.Rows(idx)

            If row.Cells(0).Value IsNot Nothing Then
                If row.Cells(0).Value.ToString() = "1" Then
                    row.DefaultCellStyle = style1

                Else
                    row.DefaultCellStyle = style2
                End If
            End If

        Next

    End Sub

0
 

Author Comment

by:webkiwi1
ID: 20366555
Awesome, I will try it right now...
0
 

Author Comment

by:webkiwi1
ID: 20366712
VBRocks, I tried the routine and it worked for the first row added, but does not work from there on out.

When the first row is added it assigns the value idx=0 and then goes through the If statement.
When the second and subsequent rows are added it jumps from the For idx to End sub and skips the If statement.

Any ideas?

idx has no value for rows 2+ but the e.rowindex increments with each row.
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 20366810
When you form loads, that event will actually get fired several times.  The first time it is fired when the
grid loads columns, and then it is fired again as the grid configures.

The final time the event is called, it will loop through all of the code and format as specified.

I tried the same exact code, no modifications, add it changed the style for any row that met the criteria.

What you want to look at is e.RowIndex and e.RowCount.
    e.RowIndex returns the index of the first row that was added.
    e.RowCount returns the number of rows that were added.


0
 

Author Comment

by:webkiwi1
ID: 20367960
Does it make a difference if the datagridview is hidden when the form loads?
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:webkiwi1
ID: 20369434
VBRocks, I have played around with this and can not get it to work. Same as explained above. I am adding rows in different places of code (sometimes 4 at a time, sometimes only 1 or two), and the DataGridView1_RowsAdded sub only applies the background color change to the very first row added to the datagridview. .
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 20369749
Sorry, just got back from the DMV...  Let me take a look.
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 20369959
Ok, well...  Sorry.  I guess I'm not going to be able to help you, because it's running perfect for me.  :(

One change I made was to search for a cell value that had a "3" in it, and it selected a lot of rows for me
(I didn't count)...  So maybe it has to do with the way your are searching?

        'Loop through all of the rows that were added,
        '  check the value of whatever column you want
        '  change the cellstyle for that row
        Dim row As DataGridViewRow

        'Index of first row added
        For idx As Int32 = e.RowIndex To e.RowCount - 1

            row = Me.DataGridView1.Rows(idx)

            If row.Cells(0).Value IsNot Nothing Then

                '* Changed this search search the cell value for "3"
                If row.Cells(0).Value.ToString().IndexOf("3") > -1 Then
                    row.DefaultCellStyle = style1

                Else
                    row.DefaultCellStyle = style2
                End If
            End If

        Next



0
 

Author Comment

by:webkiwi1
ID: 20371485
I think the problem might lie in how I am adding the rows, or where/when I am adding them.

Do you have a sample with this code and a datagridview that you are addings several rows two that you can post or maybe email so I can look at how the rows are being added?

My code has multiple sql calls that would be too confusing to sift through to test!

Thanks for trying  :)
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 20374342
Sure, I'd be happy to.

Add a new form to your project, and add a DataGridView to the form, name it "DataGridView1"
Double-Click on the form and add the code below, be sure to change the form name of "Form1" in the
"Form1_Load" event to whatever the name of your form is.

'This code will highlight every row that has a Quantity (Qty) of "3":

    Private Sub Form1_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load

        'Create a table and set it as the DataSource for the DataGridView
        Dim table As New DataTable()
        table.Columns.Add("Item")
        table.Columns.Add("Qty")

        'Add the same data 5 times so there will be 5 rows that match
        For iSets As Int16 = 1 To 5

            'Loop and add rows to table
            For i As Int16 = 1 To 5
                table.Rows.Add("Item " & i.ToString, i)
            Next

        Next

        Me.DataGridView1.DataSource = table

    End Sub



    Private Sub DataGridView1_RowsAdded(ByVal sender As System.Object, _
        ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) _
        Handles DataGridView1.RowsAdded

        'Setup all of the styles you want here
        Dim style1 As New System.Windows.Forms.DataGridViewCellStyle
        style1.BackColor = Color.Red
        style1.ForeColor = Color.Yellow

        Dim style2 As New System.Windows.Forms.DataGridViewCellStyle
        style2.BackColor = Color.White
        style2.ForeColor = Color.Black
        'End of styles


        'Loop through all of the rows that were added,
        '  check the value of whatever column you want
        '  change the cellstyle for that row
        Dim row As DataGridViewRow


        '*** Set to the name of the column to search ***
        Dim columnName As String = "Qty"

        '*** Set to the value to search for ***
        Dim searchCriteria As String = "3"

        'Index of first row added
        For idx As Int32 = e.RowIndex To e.RowCount - 1

            row = Me.DataGridView1.Rows(idx)

            'Make sure the column exists, and IsNot Nothing
            If Me.DataGridView1.Columns.Contains(columnName) = True AndAlso _
                row.Cells(columnName).Value IsNot Nothing Then

                'Search the Qty column for all cells with a value of 3
                If row.Cells(columnName).Value.ToString() = searchCriteria Then
                    row.DefaultCellStyle = style1

                Else
                    row.DefaultCellStyle = style2
                End If

            End If

        Next

    End Sub




Just a note, I tweeked the code just a bit, but not enough to make a different with the problem you
are having.

0
 

Author Comment

by:webkiwi1
ID: 20383588
The big difference I see is you are adding data to the data table, and then adding it all at once to the datagridview, whereas I am adding rows directly to the datagridview one at a time...

I will rework my code and try your method and see if it works...makes sense!
0
 
LVL 27

Expert Comment

by:VBRocks
ID: 20383619
Well, if you are adding them 1 at a time, then set the style as you add them.  Let me whip up an
example for you...

0
 
LVL 27

Accepted Solution

by:
VBRocks earned 500 total points
ID: 20383737
Ok, here's an example.

What I do:
    First, create a table - you would query your database and fill the table
    Second, loop through all of the columns in the table and add them to the DataGridView
        (You may do this manually)
    Third, loop through each row in the table and add it to the DataGridView,
        setting the style for each row if it meets the requirements.

'Here's the example:

    Private Sub Form1_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load


        'Create a table and set it as the DataSource for the DataGridView
        Dim table As New DataTable()
        table.Columns.Add("Item")
        table.Columns.Add("Qty")

        'Add the same data 5 times so there will be 5 rows that match
        For iSets As Int16 = 1 To 5

            'Loop and add rows to table
            For i As Int16 = 1 To 5
                table.Rows.Add("Item " & i.ToString, i)
            Next

        Next

        'Add columns to grid
        For Each col As DataColumn In table.Columns
            Me.DataGridView1.Columns.Add(col.ColumnName, col.ColumnName)
        Next

        'Loop through the table and add each row to the DataGridView:
        For Each row As DataRow In table.Rows
            AddRow(row)
        Next

    End Sub


    Private Sub AddRow(ByVal row As DataRow)
        'Setup all of the styles you want here
        Dim style1 As New System.Windows.Forms.DataGridViewCellStyle
        style1.BackColor = Color.Red
        style1.ForeColor = Color.Yellow

        Dim style2 As New System.Windows.Forms.DataGridViewCellStyle
        style2.BackColor = Color.White
        style2.ForeColor = Color.Black
        'End of styles

        '*** Set to the name of the column to search ***
        Dim columnName As String = "Qty"

        '*** Set to the value to search for ***
        Dim searchCriteria As String = "3"

        'Make sure the column exists, and IsNot Nothing
        If Me.DataGridView1.Columns.Contains(columnName) = True Then

            Dim index As Int32 = _
                Me.DataGridView1.Rows.Add(row.ItemArray())

            Dim gridRow As DataGridViewRow = _
                Me.DataGridView1.Rows(index)


            'Search the Qty column for all cells with a value of 3
            If gridRow.Cells(columnName).Value.ToString() = searchCriteria Then
                gridRow.DefaultCellStyle = style1

            Else
                gridRow.DefaultCellStyle = style2
            End If

        End If

    End Sub

0
 

Author Comment

by:webkiwi1
ID: 20574063
Sorry, did not get a chance to acdept solution...worked perfectly.
Thanks.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 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

18 Experts available now in Live!

Get 1:1 Help Now