2015-12-22 71 views
0

我在asp.net(vb.net)中有一個數據網格,它是從數據庫填充的,前兩個字段是一個複選框和一個按鈕圖像。在DataGrid中獲取行索引

我希望每當用戶單擊按鈕時打開另一個表單來編輯行的內容。問題在於按鈕是由visual studio爲每行動態加載的。我怎麼知道哪一行按鈕被選中,以便我可以傳遞該行的參數?

回答

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,加分新檢索行索引,

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 
...