在DataGrid中获取行索引

时间:2015-12-22 07:08:13

标签: asp.net .net vb.net datagrid

我在asp.net(vb.net)中有一个数据网格,它是从数据库中填充的,前两个字段是一个复选框和一个按钮图像。

我希望每当用户点击该按钮时,会打开另一个表单来编辑该行的内容。问题是视觉工作室为每一行动态加载按钮。如何知道从哪个行中选择按钮以便我可以传递行的参数?

1 个答案:

答案 0 :(得分:1)

您可以将DataGrid / DataGridView事件用作CellClick,CellDoubleClick,CellContentClick ...并通过e.RowIndex或e.ColumnIndex获取它。例如,

Private Sub MyDataGridView_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles MyDataGridView.CellClick

            If e.ColumnIndex = 8 Then 'only executed for cells at column 8

                Dim newForm As New MyNewForm(e.RowIndex)

                newForm.Show()

            End If

        End Sub

并在MyNewForm上添加Sub New以检索行索引

Public Class MyNewForm

    Private intRowIndex As Integer

    Public Sub New(ByVal rowIndex As Integer)

        intRowIndex = rowIndex

        ' This call is required by the designer.
        InitializeComponent()

    End Sub
...