在列中搜索datagridview返回值对应值datadgridview vb.net [英] Search in column datagridview return value coresponding value datadgridview vb.net

查看:340
本文介绍了在列中搜索datagridview返回值对应值datadgridview vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在vb.net中有一个datagridview,有三列

I have a datagridview in vb.net with three columns

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

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

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

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

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

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.

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

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)

我正在使用vb.net

I'm using 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


推荐答案

好的,谢谢代码更新。这样做:

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

这样做是循环遍历所有项目。当它找到匹配时,它将发现为true。如果没有找到项目,那么当循环结束时,found为false。如果发现为false,则显示未找到项目。希望你明白,否则问:)

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

这篇关于在列中搜索datagridview返回值对应值datadgridview vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆