每个循环的vb.net将一行与每隔一行进行比较 [英] vb.net for each loop that compares a row to every other row
问题描述
这是我到目前为止的代码。
对于每行作为DataGridViewRow在DaisyServicesForm.DataGridView1.Rows
如果row.Cells(UnitCost)。值=行。单元格(UnitCost)。值和(row.Cells(FromDate)。值< = row.Cells(ToDate)。值和row.Cells(ToDate)。值> = row.Cells (FromDate)。值)然后
row.DefaultCellStyle.ForeColor = Color.Blue
End If
Next
但是我不希望VB将行与自己进行比较,我希望它采取第一行,然后将其与其他每一行进行比较,然后取第二行将其与其他行进行比较。
如果是在SQL中,它将看起来像:
i.unitcost = i2.unitcost
和((i.FromDate< = i2.ToDate)
和(i.ToDate> = i2.FromDate))
希望这是有道理的,任何帮助不胜感激。
您需要一个内部 For Loop
如果你想让每一个项目(行)迭代并比较自己与列表(网格)中的每个其他项目。
For Each rowOuter As DataGridViewRow In DaisyServicesForm.DataGridView1.Rows
For each rowInner As DataGridViewRow In DaisyServicesForm.DataGridView1.Rows
如果rowOuter.Cells(UnitCost) .Value = rowInner.Cells(UnitCost)。值和
(rowOuter.Cells(FromDate)。值< = rowInner.Cells(ToDate)。值和rowOuter.Cells(ToDate ).Value> = rowInner.Cells(FromDate)。Value)Then
rowOuter.DefaultCellStyle.ForeColor = Color.Blue
End If
Next
Next
- 第一个外部行将与所有行进行比较。
- 下一个外部行将自己与所有行进行比较。
- ...
- 最后一行将与所有行进行比较。
您可能需要检查if语句是否存在,但这个想法应该是可行的。此外,您需要添加一个支票,以便查看该行是否正在检查。
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 In DaisyServicesForm.DataGridView1.Rows
If row.Cells("UnitCost").Value = row.Cells("UnitCost").Value And (row.Cells("FromDate").Value <= row.Cells("ToDate").Value And row.Cells("ToDate").Value >= row.Cells("FromDate").Value) Then
row.DefaultCellStyle.ForeColor = Color.Blue
End If
Next
But I don't want VB to compare rows to themselves, I want it to take first row and then compare it with every other row...and then take the second row and compare that with ever other row.
If it was in SQL it would look something like:
i.unitcost = i2.unitcost
and ((i.FromDate <= i2.ToDate)
and (i.ToDate >= i2.FromDate))
Hope that makes sense, any help greatly appreciated.
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.
这篇关于每个循环的vb.net将一行与每隔一行进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!