【问题标题】:Search in datagrid using value in textbox使用文本框中的值在数据网格中搜索
【发布时间】:2017-07-04 06:11:27
【问题描述】:

如何在 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

【问题讨论】:

    标签: vb.net datagridview datagrid


    【解决方案1】:

    您可以直接通过For Each枚举List2DataGridView.Rows,无需使用索引来访问它们。

    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")
    

    然而,这不是最优雅的解决方案,而且可读性差。具体来说,Exit For 的使用有点难看,并且使代码更难一目了然。

    我们可以通过使用 LINQ 做得更好。

    Dim Matches = From row As DataGridViewRow In List2DataGridView.rows
                  Let CellValue = row.Cells.Item(1).Value
                  Where CellValue = TextBox2.Text
                  Select New With {CellValue, row.Index}
    
    Dim Match = Matches.FirstOrDefault()
    
    If Match IsNot Nothing Then
        MsgBox("Item is found in row: " & Match.Index)
        MsgBox("Value of second column in this row: " & Match.CellValue)
    End If
    

    【讨论】:

    • 在stackoverflow上回答任何问题时请解释一下。
    • @manu 我对此进行了编辑以解释代码并提供更易读的版本,但不确定它是否回答了问题,因为我发现问题的意图不清楚
    【解决方案2】:
     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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-10
      • 1970-01-01
      • 1970-01-01
      • 2013-12-17
      • 1970-01-01
      相关资源
      最近更新 更多