Results 1 to 10 of 10

Thread: [RESOLVED] evaluating a DGV Row and change row bavkground color

  1. #1

    Thread Starter
    Addicted Member Smartacus's Avatar
    Join Date
    Oct 2009
    Location
    Deep South, USA
    Posts
    196

    Resolved [RESOLVED] evaluating a DGV Row and change row bavkground color

    My DGV's last visible column in a boolean. I am needing to go through each row and determine if the value of dgv item bFlag is true then turn the row back ground red. Here is my start of it. It appears to be only making one loop then giving the message box. I would appreciate an extra set of eyes on this. Thnx

    Code:
    Private Sub EvalGrid()
            BindDGV(dgvResults)
            Dim cnt As Integer = dgvResults.Rows.Count
    
            Try
                For x As Integer = 0 To cnt - 1
                    'MessageBox.Show(CStr(x) + " of " + CStr(cnt))
                    Dim item As String '= Number.ToString()
                    item = dgvResults.Item("personid", x).Value
    
    
                    If dgvResults.Item("bFlag", x).Value = True Then
                        With dgvResults
                            '.Rows(x).Selected = True
                            .Item("personid", x).Style.BackColor = Color.Red
                        End With
    
                    End If
                Next
            Catch
            End Try
    
            Dim editCnt As Integer = dgvResults.SelectedRows.Count
            Me.dgvResults.Focus()
            MessageBox.Show("There are " & editCnt & "  records that needs to be corrected")
    
        End Sub
    ***************************************************
    Smartacus comes packaged "As Is With No Warranty"

    ************* Useful Links ******************
    FAQs: Index / Database Development / .NET CodeBank /
    Before Posting Here...MSDN

    MZTools (I love this tool when using VB6 - Free) /

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: evaluating a DGV Row and change row bavkground color

    Use CellFormatting event to alter the background color, in your case when the column bFlag value is True as shown below.

    Code:
    Private Sub dgvResults_CellFormatting( _ 
    	ByVal sender As Object, _ 
    	ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) _ 
    	Handles dgvResults.CellFormatting
    	
        If e.ColumnIndex = dgvResults.Columns("bFlag").Index Then
            If CBool(e.Value) Then
                e.CellStyle.BackColor = Color.Red
            End If
        End If
        
    End Sub

  3. #3

    Thread Starter
    Addicted Member Smartacus's Avatar
    Join Date
    Oct 2009
    Location
    Deep South, USA
    Posts
    196

    Re: evaluating a DGV Row and change row bavkground color

    Thats cool. How do I affect the entire row?
    ***************************************************
    Smartacus comes packaged "As Is With No Warranty"

    ************* Useful Links ******************
    FAQs: Index / Database Development / .NET CodeBank /
    Before Posting Here...MSDN

    MZTools (I love this tool when using VB6 - Free) /

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: evaluating a DGV Row and change row bavkground color

    Quote Originally Posted by Smartacus View Post
    Thats cool. How do I affect the entire row?
    Chiming in from my iPad so I can not provide examples. Use Google and search on DataGridView bands. Will be back tomorrow night if you still need examples as I have some.

  5. #5

    Thread Starter
    Addicted Member Smartacus's Avatar
    Join Date
    Oct 2009
    Location
    Deep South, USA
    Posts
    196

    Re: evaluating a DGV Row and change row bavkground color

    Some success, but not there yet. The following code changes all bands True or False.

    Code:
    Private Sub dgvResults_CellFormatting( _
                            ByVal sender As Object, _
                            ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) _
                            Handles dgvResults.CellFormatting
    
            If e.ColumnIndex = dgvResults.Columns("bFlag").Index Then
    
                For Each band As DataGridViewBand In dgvResults.Rows
                    If CBool(dgvResults.Rows(band.Index).Cells("Bflag").Value) = True Then
                        band.DefaultCellStyle.BackColor = Color.BlanchedAlmond
                    End If
                Next
            End If
        End Sub
    ***************************************************
    Smartacus comes packaged "As Is With No Warranty"

    ************* Useful Links ******************
    FAQs: Index / Database Development / .NET CodeBank /
    Before Posting Here...MSDN

    MZTools (I love this tool when using VB6 - Free) /

  6. #6
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: evaluating a DGV Row and change row bavkground color

    Here is an example of using Bands that I did for another post. The original post condition was to make specific cells bold which I left and added code to make an entire row background color red.
    Attached Files Attached Files

  7. #7

    Thread Starter
    Addicted Member Smartacus's Avatar
    Join Date
    Oct 2009
    Location
    Deep South, USA
    Posts
    196

    Re: evaluating a DGV Row and change row bavkground color

    Thank you!

    I liked your Font Bold section so I also used that.

    Code:
    Public Sub ConditionsColorPrep(ByVal sender As Windows.Forms.DataGridView)
            For Row As Integer = 0 To sender.Rows.Count - 1
                If Not sender.Rows(Row).IsNewRow Then
                    If sender.Rows(Row).Cells("Bflag").Value IsNot Nothing Then
                        If sender.Rows(Row).Cells("Bflag").Value = True Then
                            sender.Rows(Row).DefaultCellStyle.BackColor = Color.BlanchedAlmond
                        End If
                    End If
                End If
            Next
        End Sub
    ***************************************************
    Smartacus comes packaged "As Is With No Warranty"

    ************* Useful Links ******************
    FAQs: Index / Database Development / .NET CodeBank /
    Before Posting Here...MSDN

    MZTools (I love this tool when using VB6 - Free) /

  8. #8
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: evaluating a DGV Row and change row bavkground color

    Quote Originally Posted by Smartacus View Post
    Thank you!

    I liked your Font Bold section so I also used that.

    Code:
    Public Sub ConditionsColorPrep(ByVal sender As Windows.Forms.DataGridView)
            For Row As Integer = 0 To sender.Rows.Count - 1
                If Not sender.Rows(Row).IsNewRow Then
                    If sender.Rows(Row).Cells("Bflag").Value IsNot Nothing Then
                        If sender.Rows(Row).Cells("Bflag").Value = True Then
                            sender.Rows(Row).DefaultCellStyle.BackColor = Color.BlanchedAlmond
                        End If
                    End If
                End If
            Next
        End Sub

    Good to hear this worked for you!

  9. #9

    Thread Starter
    Addicted Member Smartacus's Avatar
    Join Date
    Oct 2009
    Location
    Deep South, USA
    Posts
    196

    Re: [RESOLVED] evaluating a DGV Row and change row bavkground color

    You actually did more than just solve this problem. You gave a clear example of how I can streamline my code by using "Sender" for better OO coding!
    ***************************************************
    Smartacus comes packaged "As Is With No Warranty"

    ************* Useful Links ******************
    FAQs: Index / Database Development / .NET CodeBank /
    Before Posting Here...MSDN

    MZTools (I love this tool when using VB6 - Free) /

  10. #10
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: [RESOLVED] evaluating a DGV Row and change row bavkground color

    Quote Originally Posted by Smartacus View Post
    You actually did more than just solve this problem. You gave a clear example of how I can streamline my code by using "Sender" for better OO coding!
    In regards to sender within the contexts of the module DataGridViewExtensions when I declare sender as shown below

    Code:
    Public Sub InvokeBanding(ByVal sender As Windows.Forms.DataGridView)
    I prepend DataGridView with the namespace it comes from because this module was extracted from a DLL I created which by default does not have a reference to System.Windows.Forms.

    So if you are always going to work with code such as in DataGridViewExtensions in a Windows Forms project you can remove the namespace i.e.

    Code:
    Public Sub InvokeBanding(ByVal sender As DataGridView)
    While if the module might end up in a DLL keep the namespace as is now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width