vb.net for each loop that compares a row to every other row

前端 未结 1 1664
一个人的身影
一个人的身影 2021-01-24 06:21

I am trying to format some datagridview rows based on comparing them with other rows.

Here is the code I have so far.

    For Each row As DataGridViewRow         


        
相关标签:
1条回答
  • 2021-01-24 06:54

    You need an inner For Loop if you want to have each item (row) iterated through and compare itself vs every other item in the list (grid).

    For Each rowOuter As DataGridViewRow In DaisyServicesForm.DataGridView1.Rows
        For Each rowInner As DataGridViewRow In DaisyServicesForm.DataGridView1.Rows
            If rowOuter.Cells("UnitCost").Value = rowInner.Cells("UnitCost").Value And 
                (rowOuter.Cells("FromDate").Value <= rowInner.Cells("ToDate").Value And rowOuter.Cells("ToDate").Value >= rowInner.Cells("FromDate").Value) Then
                rowOuter.DefaultCellStyle.ForeColor = Color.Blue
        End If
        Next
    Next
    
    • The first outer row will compare itself to all rows.
    • The next outer row will compare itself to to all rows.
    • ...
    • The final row will compare itself to all rows.

    You probably need to check the if statement I have there, but the idea should work. Also, you need to add a check so see if it the row is checking itself.

    0 讨论(0)
提交回复
热议问题