2017-07-04 3 views
0

msgboxに値を表示するにはどうすればよいですか? DataGridの最初の列では、テキストボックスに基づいて値を探しています.2番目の列の値とmsgboxの同じ行が表示されます。今、私はあなたがFor Each経由で直接List2DataGridView.Rowsを列挙することができテキストボックスの値を使用してDataGridで検索

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 

答えて

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 
0

を「アイテムが発見」している、それらを訪問するインデックスを使用する必要はありません。

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 
+3

あなたが何か質問に答えたときに親切に説明してください。 –

+0

@manuこれを編集してコードを説明し、わかりやすいバージョンを提供しました。質問に答えるかどうかわかりませんが、質問の意図が不明なので –

関連する問題