如何在多个文本框中显示多个记录但来自同一数据库vb.net

时间:2018-12-07 13:31:06

标签: mysql vb.net

my vb purchase form output

问题在于,我想在每个文本框中选择不同的产品, 但是这些值会同等地添加到两个文本框中,因此我无法单独添加。由于我是vb的新学生,因为有一个学校项目。我确实希望有一个解决方案。我需要在3个文本框中显示3个值,但目前我仅在2texboxes中尝试过。我希望这能解释。

这是我对数据库到文本框输出的编码:

Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    Dim row As DataGridViewRow = DataGridView1.CurrentRow
    Try

        txtprod1.Text = row.Cells(1).Value.ToString()
        txtPrice.Text = row.Cells(2).Value.ToString()
        txtqty.Text = row.Cells(3).Value.ToString()

    Catch ex As Exception
        yLoad()
    End Try

    Try

        txtprod2.Text = row.Cells(1).Value.ToString()
        txtprice2.Text = row.Cells(2).Value.ToString()
        txtqty2.Text = row.Cells(3).Value.ToString()

    Catch ex As Exception
        yLoad()
    End Try

End Sub

1 个答案:

答案 0 :(得分:0)

Private Sub DataGridView1_RowEnter(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.RowEnter
        If Not IsLoaded Then
            Exit Sub
        End If
        Static CurrentCount As Integer
        If CurrentCount < 3 Then
            CurrentCount += 1
        Else
            CurrentCount = 1
        End If
        Dim s As String = DataGridView1.Rows(e.RowIndex).Cells(1).Value.ToString
        'Cell 1 happens to be the Name property in the grid I am using
        Select Case CurrentCount
            Case 1
                TextBox1.Text = s
            Case 2
                TextBox2.Text = s
            Case 3
                TextBox3.Text = s
            Case Else
                MessageBox.Show("Error")
        End Select
        'CurrentRow is a day late and a dollar short when using the RowEnter event
        'Dim s As String = DataGridView1.CurrentRow.Cells(1).Value.ToString
End Sub

Dim IsLoaded As Boolean = False

Private Sub DataGridViewTest_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
        IsLoaded = True
End Sub

IsLoaded Form级别(类级别)变量可防止选择发生,直到您更改选择为止。

静态变量将在两次Sub调用之间保留其值。

If语句跟踪Sub的调用次数,因此我们知道要填充哪个文本框。

通过使用DataGridViewCellArgs(e)和RowIndex属性,获得所需的单元格。您可以使用其他单元格值设置更多的变量,然后在选择的情况下将它们分配给文本框。