2013-06-18 3 views
5

DataGridView에서 선택한 셀을 기반으로 행 인덱스를 가져 오려고합니다. VB.NET에서 어떻게 할 수 있습니까?DataGridView에서 선택한 셀을 기반으로 RowIndex 가져 오기

이 내가 가진 무엇 :

Dim iRowIndex As Integer 
For i = 0 To Me.grdTransaction.SelectedCells.Item(iRowIndex) 
    iRowIndex = Me.grdTransaction.SelectedCells.Item(i).RowIndex.ToString() 
    Dim s As String = Me.grdTransaction.SelectedRows(i).Cells("DataGridViewTextBoxColumn6").Value 
    aList.Add(s) 

    MsgBox("Row index " & iRowIndex) 
Next 

답변

8

덕분에 내가 알아 낸 @matzone합니다 :

Dim iRowIndex As Integer 

    For i As Integer = 0 To Me.grdTransaction.SelectedCells.Count - 1 
    iRowIndex = Me.grdTransaction.SelectedCells.Item(i).RowIndex 
    aList.Add(Me.grdTransaction.Rows(iRowIndex).Cells("DataGridViewTextBoxColumn6").Value) 
    MsgBox("Row index " & Format(iRowIndex)) 
    Next 
0

당신이 시도 할 수 있습니다 ..

Dim iRowIndex As Integer 
Dim s As String 

For i as Integer = 0 To Me.grdTransaction.SelectedCells.Count -1 

    iRowIndex = Me.grdTransaction.SelectedCells.Item(i).RowIndex.ToString() 
    aList.Add(Me.grdTransaction.SelectedRows(i).Cells("DataGridViewTextBoxColumn6").Value) 

    MsgBox("Row index " & format(iRowIndex)) 
Next 
+0

감사를 작동합니다. 나는 ArgumentsOutofRange 예외를 얻고있다. – alwaysVBNET

+0

@nectarines .. argh .. 나는 그것을 잊어 버렸다. .. – matzone

3

내가 질문을 이해 해요 생각하지 않습니다. 이유는 무엇입니까

iRowIndex = grdTransaction.SelectedRow.RowIndex 

작동하지 않습니까?

+0

내가 셀을 선택했기 때문에 행이 아님 – alwaysVBNET

+0

사용자가 개별 셀을 선택할 수있는 GridView를 만들지 않았기 때문에 나를 용서해 준다. 하지만 확실하게 특정 셀을 선택한 경우에도 SelectedRow.RowIndex (또는 더 나은 여전히 ​​SelectedRow) 속성은 선택된 셀이있는 행의 인덱스입니다. –

+0

네, 틀림 없습니다. – alwaysVBNET

4

DGV.CurrentRow.Index

는 이동을위한 경우에도 selectionMode = CellSelect

관련 문제