在列中搜索datagridview返回值对应值datadgridview vb.net

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19223852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:12:26  来源:igfitidea点击:

Search in column datagridview return value coresponding value datadgridview vb.net

vb.netsearchdatagridview

提问by ABANDOND ACOUNT

I have a datagridview in vb.net with three columns

我在 vb.net 中有一个包含三列的 datagridview

in the first is a product description, in the second is a product number and in the 3rd a price

第一个是产品描述,第二个是产品编号,第三个是价格

I would like to search in a datagridview by product number and return the corresponding value in the prices column.

我想按产品编号在 datagridview 中搜索并在价格列中返回相应的值。

I am able to search for a text in datagridviews but am at current unable to read the value of a corresponding cell such as the price cell.

我能够在 datagridviews 中搜索文本,但目前无法读取相应单元格(例如价格单元格)的值。

So to recap, I want to search for a product number but return the products price (to be stored in a variable for further use)

所以回顾一下,我想搜索产品编号但返回产品价格(存储在变量中以供进一步使用)

I'm using vb.net

我正在使用 vb.net

EDDIT:

编辑:

Private Sub Button33_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button33.Click
    Dim rowindex As String
    For Each row As DataGridViewRow In DataGridView1.Rows
        If row.Cells.Item("ITEM_ID").Value = TextBox5.Text Then
            rowindex = row.Index.ToString()

            Dim actie As String = row.Cells("PRICE").Value.ToString()
            MsgBox(actie)

        Else

            MsgBox("Item not found")

        End If
    Next
End Sub

回答by WozzeC

Allright, thanks for the code update. Do this:

好的,感谢您的代码更新。做这个:

Private Sub Button33_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button33.Click
    Dim rowindex As String
    Dim found as Boolean = false
    For Each row As DataGridViewRow In DataGridView1.Rows
        If row.Cells.Item("ITEM_ID").Value = TextBox5.Text Then
            rowindex = row.Index.ToString()
            found = true
            Dim actie As String = row.Cells("PRICE").Value.ToString()
            MsgBox(actie)
            Exit for
        End If
    Next
    If Not found Then
       MsgBox("Item not found")
    End if    
End Sub

What this does is that it loops through all the items. When it finds a match it sets found to true. If not item is found then "found" is false when the loop ends. And if "found" is false then you display "Item not found". Hope you understand, otherwise ask :)

它的作用是遍历所有项目。当它找到匹配项时,它会将 found 设置为 true。如果未找到项目,则在循环结束时“找到”为假。如果“找到”为假,则显示“未找到项目”。希望你明白,否则问:)