如何将datagridview中的第二个选定行传递给文本框?

时间:2019-01-13 07:04:43

标签: vb.net winforms datagridview

如何将我在datagridview中选择的第二行传递给textboxt。我只知道如何放入第一个数据。如何将第二个选择传递给文本框?

    Dim i As Integer

    i = DataGridView2.SelectedRows(0).Index

    Me.txtEmployeeID.Text = DataGridView2.Item(0, i).Value

3 个答案:

答案 0 :(得分:0)

您可以使用对我有用的这段代码

Dim test As String = DataGridView1.SelectedRows(0).Cells(2).Value.ToString

您只需要更改.cell(Here write the index value of the cell)

然后您可以使用字符串值填充文本框

答案 1 :(得分:0)

尝试使用以下内容:

Dim secondRow as integer = 0

Private Sub DataGridView2_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView2.CellClick
    'assuming that by second selected row, you mean the row after the selected row
    secondRow = e.RowIndex + 1
End Sub

Private Sub RowToTextBox()
    Try
        For i = 0 to DataGridView2.ColumnCount - 1
            txtEmployeeID.Text &= DataGridView2.Item(i, secondRow)
        Next
    Catch ex as Exception
        MsgBox("You have selected the final row")
    End Try
End Sub

以相同形式引用文本框时,我认为不需要Me.

答案 2 :(得分:0)

由于您不知道用户将选择多少行,因此我认为循环可能会导致SelectedRows集合起作用。我使用了“名称”列,因为这正是我在网格中所拥有的。代替MessageBox,您可以将值添加到ListBox。

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    For Each GridRow As DataGridViewRow In DataGridView1.SelectedRows
        MessageBox.Show($"Value is {GridRow.Cells("Name").Value}")
    Next
End Sub