单击下一步按钮,将在文本框中更新要更新的新值

时间:2014-11-16 18:21:02

标签: vb.net

在下面的代码中,我想将Button7用作下一个按钮。此按钮获取查询结果并将其显示在文本框中(说明)。现在由于对同一查询有多个描述,我希望在单击时按钮显示datagridview中的下一个描述。

Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
    Dim conn5 As MySqlConnection
    conn5 = New MySqlConnection
    conn5.ConnectionString = "server=127.0.0.1;userid=dec;password=xVdRs84825uhLZtb;database=dec"
    Dim sda5 As New MySqlDataAdapter
    Dim dbdataset5 As New DataTable
    Dim bsource5 As New BindingSource
    Try
        conn5.Open()
        Dim command As New MySqlCommand
        ' Dim sqlquery As String = "SELECT comment FROM bh_status WHERE jobid = '" & jobid & "';"
        Dim sqlquery As String = "SELECT item_code,description,unit,quantity,material_cost,labour_cost,total_material_cost,total_labour_cost FROM ht_33kv_list_qty where project_id = '" & prj_id & "';"
        command = New MySqlCommand(sqlquery, conn5)
        sda5.SelectCommand = command
        sda5.Fill(dbdataset5)
        bsource5.DataSource = dbdataset5
        DataGridView4.DataSource = bsource5
        sda5.Update(dbdataset5)
        conn5.Close()
        Dim i As Integer = 0
        For i = 0 To DataGridView4.RowCount - 1

            Dim cell As DataGridViewCell = DataGridView1(3, i)
            Description.Text = cell.Value


        Next
    Catch ex As Exception

    End Try
End Sub

1 个答案:

答案 0 :(得分:0)

描述文本框中显示 DataGridView1 第3列中所有单元格的值,一次点击 Button7 ,您可以执行以下操作:

Private Sub Button7_Click(sender As System.Object, e As System.EventArgs) Handles Button7.Click
    With Me.DataGridView1
        Dim NextRow = .CurrentRow.Index + 1
        If NextRow >= .Rows.Count Then
            NextRow = .Rows.Count - 1   ' If you need a cycle then using ─────> NextRow = 0
        End If
        .CurrentCell = Me.DataGridView1(.CurrentCell.ColumnIndex, NextRow)
    End With
End Sub

Private Sub DataGridView1_CurrentCellChanged(sender As Object, e As System.EventArgs) Handles DataGridView1.CurrentCellChanged
    With Me.DataGridView1
        If .CurrentCell IsNot Nothing Then
            Me.Description.Text = Me.DataGridView1(3, .CurrentCell.RowIndex).Value.ToString
        End If
    End With
End Sub