2015-12-21 50 views
2

我已閱讀有關此主題的帖子的分數,但沒有一個似乎解決了我的特定問題。我正在使用一個帶有vb的wpf網格。這是我的事件處理程序:如何從DataGridCellInfo檢索行索引

Private Sub dgrdMain_SelectedCellsChanged(sender As Object, e As SelectedCellsChangedEventArgs) Handles dgrMain.SelectedCellsChanged 
    If e.RemovedCells.Count > 0 Then 
     Dim ilRemovedCells As IList(Of DataGridCellInfo) = e.RemovedCells 
     For Each rc As DataGridCellInfo In ilRemovedCells 
      rc.Column.DisplayIndex 'This gives the column index 
      'How do I get the row index? 
     Next 
    End If 

這裏rc =已刪除的單元格。我的問題是上面的註釋行。

回答

2

這裏是一個快速函數返回指數...讓我知道這是你需要什麼...

Public Shared Function GetRowIndex(dataGrid As DataGrid, dataGridCellInfo As DataGridCellInfo) As Integer 
Dim dgrow As DataGridRow = DirectCast(dataGrid.ItemContainerGenerator.ContainerFromItem(dataGridCellInfo.Item), DataGridRow) 
If dgrow IsNot Nothing Then 
    Return dgrow.GetIndex() 
    Else 
    Return -1 
End If 

End Function 
+0

優秀,Codexer。 – SezMe

+0

很高興爲你工作。 – Codexer