使用VB.NET从DataGrid中的选定行获取数据

时间:2016-09-29 00:57:45

标签: asp.net .net vb.net

我有一个datagrid,我希望所选行中的数据显示在textbox中。我正在使用.NET 2003。我发现的只是datagridview的解决方案。我尝试了此代码以及SelectedRows,但datagrid中没有该函数。

Private Sub Grid2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Grid2.SelectedIndexChanged
    Dim i, j As Integer
    i = Grid2.CurrentRow.Index
    TextBox1.Text = Grid2.Item(0, i).Value
    TextBox2.Text = Grid2.Item(1, i).Value
    TextBox3.Text = Grid2.Item(2, i).Value
    TextBox4.Text = Grid2.Item(3, i).Value
End Sub

1 个答案:

答案 0 :(得分:1)

试试这个:

Private Sub Grid2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Grid2.SelectedIndexChanged
    TextBox1.Text = Grid2.SelectedItem.Cells(0).Text
    TextBox2.Text = Grid2.SelectedItem.Cells(1).Text
    TextBox3.Text = Grid2.SelectedItem.Cells(2).Text
    TextBox4.Text = Grid2.SelectedItem.Cells(3).Text
End Sub