我有一个datagridview(vb.net,winforms),我在其中读取excel表中的数据, 现在,如果一行具有值“代码”而另一行具有相同列中的相同值,那么
如何使用linq或任何其他方法计算这两个单元格之间的行数。?
答案 0 :(得分:0)
这将计算特定值的两次出现之间的行:
Private Sub DGVSearchTestButton_Click(sender As System.Object, e As System.EventArgs) Handles DGVSearchTestButton.Click
Try
Dim dt As DataTable = New DataTable
dt.Columns.Add(New DataColumn("CodeValue"))
addRow(dt, "100")
addRow(dt, "200")
addRow(dt, "300")
addRow(dt, "400")
addRow(dt, "100")
addRow(dt, "200")
addRow(dt, "300")
dgvSearch.DataSource = dt
Debug.Print(countRowsBetween(0, "200"))
Catch ex As Exception
MessageBox.Show(String.Concat("An error occurred: ", ex.Message))
End Try
End Sub
Private Sub addRow(dt As DataTable, rowValue As String)
Dim dr As DataRow = dt.NewRow
dr.Item("CodeValue") = rowValue
dt.Rows.Add(dr)
End Sub
Private Function countRowsBetween(columnNumber As Integer, valueToSearchFor As String) As Integer
Dim rowCount As Integer = -1
Dim findFirst As Boolean
Dim findSecond As Boolean
Dim searchFromRow As Integer
For rowParse As Integer = 0 To dgvSearch.RowCount
If dgvSearch.Rows(rowParse).Cells(columnNumber).Value.ToString = valueToSearchFor Then
findFirst = True
searchFromRow = rowParse + 1
Exit For
End If
Next
If findFirst Then
For rowParse As Integer = searchFromRow To dgvSearch.RowCount
If dgvSearch.Rows(rowParse).Cells(columnNumber).Value.ToString = valueToSearchFor Then
findSecond = True
rowCount = rowParse - searchFromRow
Exit For
End If
Next
End If
Return rowCount
End Function