0

我是 VB.net(编程)的新手。我需要你关于 Gridview 控制的建议。

我有一个加载了两列的gridview - 一列(名称)带有一些文本,另一列(价格)是空的。

我有一个包含名称和价格数据的文本框。

现在,我想遍历文本框,看看 GridView 控件的列(名称)的数据/符号是否与文本框中的数据匹配。

如果 GridView 的第一列数据的名称与文本框的名称匹配,则应在 GridView 的第二列(价格)中获取价格数据。

为了更清楚,说:

我在 Textbox 中有以下数据:

名称-价格

AB- 50

DE- 80

我在 GridView 中有两列,设置如下:

名称(column1) – 价格(column2)

AB- 空
DE- 空

现在,如何获取文本框的价格数据,并将它们提取到与 Column1 的名称匹配的 Gridview 的 Column2 中。因此,GridView 中的输出应该是:

名称(column1) – 价格(column2)

AB- 50
DE- 80

到目前为止,我已经能够遍历 GridView 的第一列……..我不确定如何从 Textbox 获取数据并将数据提取到 GridView 的 Column2 中。

任何建议将不胜感激。

公开课形式1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'Data of GridView
    Dim dt As New DataTable()
    dt.Columns.Add("Name")
    dt.Columns.Add("Price")
    dt.Rows.Add(New [Object]() {"AB"})
    dt.Rows.Add(New [Object]() {"DE"})
    Me.DataGridView1.DataSource = dt


    'Data of Textbox
    TextBox1.Text = "AB, 50" & vbNewLine & "DE, 100"


End Sub




Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'loop through the gridview
    Dim marketvalue As String
    For Each r As DataGridViewRow In Me.DataGridView1.Rows
        marketvalue = r.Cells(0).Value
        MessageBox.Show(marketvalue)
    Next
End Sub End Class
4

1 回答 1

0

我会使用一个单独的函数从您的 TextBox 中获取价格

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'loop through the gridview
    Dim marketvalue As String
    Dim price As String
    For Each r As DataGridViewRow In Me.DataGridView1.Rows

        marketvalue = r.Cells(0).Value
        MessageBox.Show(marketvalue)

        'Get the price by calling a function and update it back to the DataGrid
        price = get_price(marketvalue)
        r.Cell(1).Value = price
    Next
End Sub 

下面的函数可以通过传递name参数返回价格,如果没有找到name则为空

Private Function get_price(ByVal strName As String) As String

    Dim string_array() As String = TextBox1.Text.Split(System.Environment.vbNewLine)
    For Each s As String In string_array
        Dim string_detail() As String = s.Split(",")
        If string_detail(0) = strName Then
            Return Trim(string_detail(1))
        End If
    Next

    Return ""
End Function
于 2012-05-22T04:06:32.663 回答