Results 1 to 7 of 7

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
    38,943

    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
    38,943

    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

  5. #5

    Re: Compare DGV Row Value

    it's hard to understand.

  6. #6

    Re: Compare DGV Row Value

    Quote Originally Posted by qulaitks View Post
    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. seo tools for agencies
    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
    It seems like you want to highlight rows in a DataGridView where the transaction is of type "Buy" and there is no corresponding "Sell" transaction with the same Transaction ID. Your current code attempts to compare the Transaction ID of different rows, and if they don't match, it highlights the entire row. To achieve your goal, you can modify your code as follows:

    Code:
    For i As Integer = 0 To DGV_Trans.Rows.Count - 1
        Dim currentID As String = DGV_Trans.Rows(i).Cells(0).Value.ToString()
        Dim currentType As String = DGV_Trans.Rows(i).Cells(1).Value.ToString()
        Dim isBuyTransaction As Boolean = (currentType = "Buy")
        Dim hasCorrespondingSell As Boolean = False
    
        ' Check if there is a corresponding "Sell" transaction for this "Buy" transaction
        For j As Integer = 0 To DGV_Trans.Rows.Count - 1
            If i <> j Then
                Dim otherID As String = DGV_Trans.Rows(j).Cells(0).Value.ToString()
                Dim otherType As String = DGV_Trans.Rows(j).Cells(1).Value.ToString()
                
                If currentID = otherID AndAlso otherType = "Sell" Then
                    hasCorrespondingSell = True
                    Exit For
                End If
            End If
        Next
    
        ' Highlight the row if it's a "Buy" transaction without a corresponding "Sell"
        If isBuyTransaction AndAlso Not hasCorrespondingSell Then
            DGV_Trans.Rows(i).DefaultCellStyle.BackColor = Color.Green
        End If
    Next
    This code iterates through the DataGridView and checks if each "Buy" transaction has a corresponding "Sell" transaction with the same Transaction ID. If not, it highlights the "Buy" transaction row.
    Last edited by karasmithson; Oct 13th, 2023 at 05:49 AM.

  7. #7
    New Member
    Join Date
    Jan 2024
    Posts
    1

    Re: Compare DGV Row Value

    Have you considered modifying your code to iterate through the DataGridView and highlight rows where the transaction is of type "Buy" and there is no corresponding "Sell" transaction with the same Transaction ID, rather than comparing Transaction IDs between different rows?

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