dcsimg
Results 1 to 17 of 17

Thread: VS2013 Datagridview click to change color

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2018
    Posts
    9

    VS2013 Datagridview click to change color

    We have a form in app in development that has a datagridview that is being used to help our inventory people. The request is simple, we have 5 colors, we would like to be able to click the cell (or row index on far left) and have the entire row change it's background color to the 1st color in the cycle. When that same row/cell is clicked again, it would cycle to the next colr, etc etc. Below is the code I have so far, somewhat gleaned from other web sources, but it does not appear to work.


    Code:
     Private Sub grd_Report_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles grd_Report.CellContentClick
            If grd_Report.CurrentCell.Style.BackColor = Color.White Then
                grd_Report.CurrentRow.DefaultCellStyle.BackColor = Color.Blue
            End If
    
            If grd_Report.CurrentCell.Style.BackColor = Color.Blue Then
                grd_Report.CurrentRow.DefaultCellStyle.BackColor = Color.Lime
            End If
    
            If grd_Report.CurrentCell.Style.BackColor = Color.Lime Then
                grd_Report.CurrentRow.DefaultCellStyle.BackColor = Color.Yellow
            End If
    
            If grd_Report.CurrentCell.Style.BackColor = Color.Yellow Then
                grd_Report.CurrentRow.DefaultCellStyle.BackColor = Color.Red
            End If
    
            If grd_Report.CurrentCell.Style.BackColor = Color.Red Then
                grd_Report.CurrentRow.DefaultCellStyle.BackColor = Color.White
            End If
    
        End Sub
    Any assistance woudl be appreciated.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,369

    Re: VS2013 Datagridview click to change color

    The first thing to check is whether or not the event is being raised, and whether or not the correct selection is being made. A breakpoint and a bit of stepping will confirm this.

    I was going to show you an example from my code, but I realize that what I'm doing is different in a potentially significant way. What I am doing is handling the CellFormatting event of the DGV. In this event, I check the value of a cell, and change the backcolor depending on what I find. However, I am changing the backcolor of the current cell with a line like this:

    e.CellStyle.BackColor = Drawing.Color.LightYellow

    And that's where the difference comes in, but the more I think about it, the more I think the difference may not matter. I'm doing things based on columns, so I check e.ColumnIndex. You are wanting to do things by row, so you'd be checking e.RowIndex.

    So, you may find that switching to handling the CellFormatting event will do. You wouldn't be checking the CurrentCell anymore, though. You'd be checking grd_Repoert.Rows(e.RowIndex).Cells(<your significant cell here>).BackColor.
    My usual boring signature: Nothing

  3. #3
    Fanatic Member
    Join Date
    Sep 2017
    Posts
    545

    Re: VS2013 Datagridview click to change color

    You just want to 'toggle' colors? I dont see why you need to check the cell backcolor, just use an array
    IE
    Code:
            Dim ColorAry() As Drawing.Color = {Color.Blue, Color.Lime, Color.Yellow, Color.Red, Color.White}
            If CurrColor = 5 Then 'CurrColor initialized in class declaration 
                CurrColor = 0
            End If
            DGV_Employees.CurrentRow.DefaultCellStyle.BackColor = ColorAry(CurrColor)
            CurrColor += 1
            DGV_Employees.ClearSelection()

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2018
    Posts
    9

    Re: VS2013 Datagridview click to change color

    Yes, we want them to toggle to the next color in the sequence each time the cell is clicked/selected. While I use VB to an extent, I have never trusted it's arrays that much.

    As for the formatting check, we need all cells/rows to come up white (default) when the DGV is initially filled, as this is an e-version of a serial number cycle count system.When an employee is going through them, we want a way for them to be able to quickly and easily tell where they are if they get pulled away, and when done so we can tell what ones we need to fix/address.

    I wil put in a breakpoint though and see if the code is properly handling the click event. Thank you both for your insight thus far

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,369

    Re: VS2013 Datagridview click to change color

    I don't have a huge amount of experience with coloring DGVs, but the CellFormatting event has worked for me, and should work for you, so if you don't find an obvious issue, you can switch.

    Don't worry about arrays in pretty nearly any language. They are just too fundamental for a language to get them wrong and survive.
    My usual boring signature: Nothing

  6. #6
    Fanatic Member
    Join Date
    Sep 2017
    Posts
    545

    Re: VS2013 Datagridview click to change color

    I have never trusted it's arrays that much.
    Have you had a bad experience?

  7. #7
    Fanatic Member
    Join Date
    Sep 2017
    Posts
    545

    Re: VS2013 Datagridview click to change color

    Here is something like what you have.
    Code:
        Private Sub DataGridView1_CellClick_1(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
            DataGridView1.CurrentRow.DefaultCellStyle.BackColor = CurrentClr(DataGridView1.CurrentRow.DefaultCellStyle.BackColor)
            DataGridView1.ClearSelection()
        End Sub
        Private Function CurrentClr(ByVal ColorVal As Drawing.Color) As Drawing.Color
            Select Case ColorVal
                Case Color.White, Color.Empty
                    Return Color.Blue 'Or what ever
                Case Color.Blue
                    Return Color.White 'Or what ever
                Case Color.Lime 'Color.Lime
                    Return Color.Red
                Case Color.Yellow
                    Return Color.Yellow
                Case Color.Yellow
                    Return Color.Lime
                Case Color.Red
                    Return Color.Blue
            End Select
         
        End Function
    Last edited by kpmc; Mar 12th, 2018 at 07:55 PM.

  8. #8

    Thread Starter
    New Member
    Join Date
    Mar 2018
    Posts
    9

    Re: VS2013 Datagridview click to change color

    After putting in a few breakpoints, I can verify that the original code is intercepting the click in a cell, and it's reporting back the default white background, but apparently isn't doing anything with it.

    I will try the code you gave me Kpmc and see fi that worked.

    And yes, when I took VB back in College, the teacher launched in to 'multi dimensional' arrays that were just brutal, and not fun, even though the concept is simple enough. But I will try some of these out and let you all know if it solves the issue.

  9. #9

    Thread Starter
    New Member
    Join Date
    Mar 2018
    Posts
    9

    Re: VS2013 Datagridview click to change color

    Thank you KPMC, A combination of your code (slightly modified) and changing the function to be CellClick instead of CellContentClick has the issue sorted. I thank you all for your help in this. One quick question remains (apologies) When I call this form, which does work, it comes up, populates, then instantly goes back behind the main form. The code to call it is below, but this is a minor issue.

    Code:
    Private Function StartReport(ByVal EnteredItem as String) As Integer
         frm_report.startItem = tb_ItemToReportOn
         frm_report.startDate = CType(dt_StartDate.Text, String)
         frm_report.Show()
         frm_report.Focus()
         tb_ItemToReportOn.Clear()
         tb_ItemToReportOn.Focus()
    
         Return 0
    End Function
    Should I just move the clearing of the fields to the calling function to alleviate the issue?

  10. #10
    Hyperactive Member
    Join Date
    Nov 2017
    Posts
    398

    Re: VS2013 Datagridview click to change color

    Have you considered that the code is properly performing exactly as written, but that you have not written it to perform exactly as desired?

    Step through your code.

    Code:
            If grd_Report.CurrentCell.Style.BackColor = Color.White Then
                grd_Report.CurrentRow.DefaultCellStyle.BackColor = Color.Blue
            End If
    
            If grd_Report.CurrentCell.Style.BackColor = Color.Blue Then
                grd_Report.CurrentRow.DefaultCellStyle.BackColor = Color.Lime
            End If
    
            If grd_Report.CurrentCell.Style.BackColor = Color.Lime Then
                grd_Report.CurrentRow.DefaultCellStyle.BackColor = Color.Yellow
            End If
    
            If grd_Report.CurrentCell.Style.BackColor = Color.Yellow Then
                grd_Report.CurrentRow.DefaultCellStyle.BackColor = Color.Red
            End If
    
            If grd_Report.CurrentCell.Style.BackColor = Color.Red Then
                grd_Report.CurrentRow.DefaultCellStyle.BackColor = Color.White
            End If
    No matter what the backcolor starts at, at the end of the sub it will cycle all the way through back to white. This is because your If statements are all standalone if statements and not if-else statements. If it starts out white, it will turn blue, but then the very next if statement checks if it is blue (which it will be) and change it to lime. Then the very next if statement checks if it is lime (which it will be) and change it to yellow, yellow to red, red to white. All in one execution of the Sub. So you either need if-else statements (or a select case statement as indicated in a previous post), or you need to drop an "Exit Sub" statement inside of each If block right after the new color is assigned.

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,369

    Re: VS2013 Datagridview click to change color

    HA! I didn't pick up on that. It's true, though.

    There certainly is a pretty important difference between CellClick and CellContentsClick, but what OptionBase1 pointed out is a pretty important point.

    As to multidimensional arrays, I can understand your reluctance with them. There is almost never a reason to use those anymore. There are a few situations where they are the right tool, but more often, an array of some class does what is really desired these days. Single dimension arrays have always been, and still remain, considerably simpler. There are more ways to work with them in .NET than there was in VB6, but they were always conceptually easier than multidimensional arrays, and that is still the case.
    My usual boring signature: Nothing

  12. #12
    Fanatic Member
    Join Date
    Sep 2017
    Posts
    545

    Re: VS2013 Datagridview click to change color

    I use ShowDialog which will not allow focus on anything but the opening form.

    Code:
        Private Sub OpenForm_Click(sender As Object, e As EventArgs) Handles OpenForm.Click
            Dim SomeForm As New Form1
            SomeForm.ShowDialog()
        End Sub

  13. #13
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,369

    Re: VS2013 Datagridview click to change color

    Yeah, but if you need non-modal, then you need non-modal, in which case ShowDialog won't work for you.
    My usual boring signature: Nothing

  14. #14
    Fanatic Member
    Join Date
    Sep 2017
    Posts
    545

    Re: VS2013 Datagridview click to change color

    Quote Originally Posted by Shaggy Hiker View Post
    Yeah, but if you need non-modal, then you need non-modal, in which case ShowDialog won't work for you.
    Im not real sure why you would need non-modal?

  15. #15

    Thread Starter
    New Member
    Join Date
    Mar 2018
    Posts
    9

    Re: VS2013 Datagridview click to change color

    I moved the clearing back up to the calling function, changed the ShowReport Function to a SUB(no floating unhandled return values in the memory space now), and used the showdialog. It works as intended now. I need to finish shaking the rust out of my VB....

  16. #16
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,369

    Re: VS2013 Datagridview click to change color

    Quote Originally Posted by kpmc View Post
    Im not real sure why you would need non-modal?
    I certainly use modal far more than non-modal, but there's a time and place for everything. You use modal when you want to have one form showing one thing, while a different form is showing a different thing, and you need to be able to work on both of them.
    My usual boring signature: Nothing

  17. #17
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,217

    Re: VS2013 Datagridview click to change color

    Quote Originally Posted by ThetaCoder View Post
    We have a form in app in development that has a datagridview that is being used to help our inventory people. The request is simple, we have 5 colors, we would like to be able to click the cell (or row index on far left) and have the entire row change it's background color to the 1st color in the cycle. When that same row/cell is clicked again, it would cycle to the next colr, etc etc. Below is the code I have so far, somewhat gleaned from other web sources, but it does not appear to work.

    Any assistance woudl be appreciated.
    I don't get the logic in the above ? if it for a Inventory then the color of the row could(should be based on a value in some Cell..
    1= inActive
    2= need to Order
    3= running out soon
    etc....

    here a sample...
    Code:
     Private Sub ApplyFormatting()
     For Each row As DataGridViewRow In DataGridView1.Rows
    
                'check what is in Cell 2
                Select Case CInt(row.Cells(2).Value)
                    Case Is = 1 'inActive
                        row.Cells(2).Style.BackColor = Color.Aquamarine
                        
                    Case Is = 2 'need to Order
                        row.Cells(2).Style.BackColor = Color.Pink
    'etc...
                  
                End Select
            Next
    
    End Sub

    regards
    Chris
    Last edited by ChrisE; Mar 14th, 2018 at 02:49 AM.
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width