Results 1 to 4 of 4

Thread: Compare DGV Row Value

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2020
    Posts
    2

    Compare DGV Row Value

    Good Day , H have Transactions DGV that holding Same Transaction ID for both type (Buy /Sell). This is on Cell(0)of the DGV. I want to highlight the transaction that bought but not sold , Cell(1) has the transaction type. I have tried the below cod but it is highlighting the whole rows. Please Help.
    Code:
    For i As Integer = 0 To DGV_Trans.Rows.Count - 2
                For j As Integer = i + 1 To DGV_Trans.Rows.Count - 2
    
                    If DGV_Trans.Rows(i).Cells(0).Value <> DGV_Trans.Rows(j).Cells(0).Value Then
                        DGV_Trans.Rows(j).DefaultCellStyle.BackColor = Color.Green
    
                    End If
    
                Next
            Next
    Last edited by Shaggy Hiker; May 29th, 2020 at 09:09 AM. Reason: Added CODE tags.

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

    Re: Compare DGV Row Value

    It doesn't JUST highlight the row. There must be some error message. I can guess at what it is, but why don't you tell us.

    It's probably that you haven't looked at the types in the comparison.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2020
    Posts
    2

    Re: Compare DGV Row Value

    there is no error message. I only want to check Cell(0) if the number is not repeated then highlight the row.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,083

    Re: Compare DGV Row Value

    Have you stepped through the code?

    I just realized that I might have misunderstood your question. Would setting the color to green be highlighting or not highlighting? As long as that If statement is working (and the line inside the If statement is reached), then it seems like that should set the backcolor for the entire row to green. That would be a slightly odd highlight color, but if that IS the highlight color, then I'm not sure what problem you are trying to solve.

    I was thinking that you were comparing two Objects using <>, which won't work, but if the comparison is working, then those aren't type Object. Still, I can't decide whether you are saying that it's highlighting or not highlighting, whether it's highlighting too many rows, or the wrong rows, or shouldn't be highlighting the entire row. If green is the highlight, then what you are doing will highlight something, but it seems like it would highlight pretty nearly everything other than the final row (since you always go to count-2, you never check the final row). In fact, that code seems very likely to highlight every row aside from the first and the last every time, in which case it will also highlight nothing if there are only two rows, and sometimes would highlight nothing in other cases, as well.

    So, I guess the bottom line is that the way the question is written, you are assuming that we know thing about the design that only you know. We don't know what highlighting means. We don't know whether setting a backcolor to green is highlighting or not. We don't know whether there is a pattern to the data that makes it reasonable to skip the first and last rows. We don't know whether there's a pattern to the data such that the code won't end up just setting everything to green aside from the first and last rows. After all, you are comparing on inequality and talking about "the same transaction ID". If they are all unique, and the first line holds a value like "2", then all the other lines will not be "2", so they will all be turned green...and that's only after the first time through the outer loop. Basically, everything would be set to green unless all the values are the same. Is that what they should be? We certainly don't know, but you do.
    My usual boring signature: Nothing

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