使用文本框中的值在d​​atagrid中搜索

时间:2017-07-04 06:11:27

标签: vb.net datagridview datagrid

如何在msgbox中显示值。在datagrid的第一列中,基于文本框查找值,我希望显示第二列的显示值和msgbox中的同一行。现在我只有“找到物品”

Here are columns

Private Sub PictureBox12_Click(sender As Object, e As EventArgs) Handles PictureBox12.Click


        Dim temp As Integer = 0
        For i As Integer = 0 To List2DataGridView.RowCount - 1
            For j As Integer = 0 To List2DataGridView.ColumnCount - 1
                If List2DataGridView.Rows(i).Cells(j).Value.ToString = TextBox2.Text Then
                    MsgBox("Intem found")
                    temp = 1
                End If
            Next
        Next
        If temp = 0 Then
            MsgBox("Item not found")
        End If
    End Sub

2 个答案:

答案 0 :(得分:0)

 Private Sub PictureBox12_Click(sender As Object, e As EventArgs) Handles PictureBox12.Click

        Dim barcode As String
        Dim rowindex As String
        Dim found As Boolean = False
        barcode = InputBox("Naskenujte čárový kód ||||||||||||||||||||||||||||||||||||")
        If Len(Trim(barcode)) = 0 Then Exit Sub   'Pressed cancel

        For Each row As DataGridViewRow In List2DataGridView.Rows
            If row.Cells.Item("DataGridViewTextBoxColumn1").Value = barcode Then
                rowindex = row.Index.ToString()
                found = True
                Dim actie As String = row.Cells("DataGridViewTextBoxColumn2").Value.ToString()
                MsgBox("Čárový kód:  " & barcode & vbNewLine & "Číslo dílu je:  " & actie, , "Vyhledání dílu")

                Exit For
            End If
        Next
        If Not found Then
            MsgBox("Item not found")
        End If
    End Sub

答案 1 :(得分:0)

您可以直接通过function setDiameter() { links.forEach(function (link) { svg.selectAll("circle") .attr("r", function (d) { if (d === link.source) { return 15; } else return 6; }) }); } 枚举List2DataGridView.Rows,无需使用索引来访问它们。

For Each

然后,对于每一行,我们测试它的值,当我们找到匹配的行时,我们会显示一条包含其值的消息。我们可以访问该值,因为它在范围内。当我们找到匹配元素时,我们退出For Each row As DataGridViewRow In List2DataGridView.Rows

For Each

然而,这不是最优雅的解决方案,并且可读性差。具体来说,For Each row As DataGridViewRow In List2DataGridView.Rows If row.Cells.Item(1).Value = TextBox2.Text Then MsgBox("Item is found in row: " & row.Index) MsgBox("Value of second column in this row: " & row.Cells.Item(1).Value) Exit For End If Next MsgBox("Item not found") 的使用有点难看,使代码更难以克服。

使用LINQ可以做得更好。

Exit For