错误:确保索引小于集合的大小

时间:2017-02-03 00:45:53

标签: vb.net

当我单独选择/点击前两行时,我很好。如果我选择/点击之后的任何行,我会收到错误。错误消息是: "指数超出范围。必须是非负的且小于集合的大小" 我添加了一个RowIndexCount来计算应用程序正在读取的行数,并返回84.它可能出现什么问题? VB.NET

    Dim rowDGVDataPhys As Integer = DGVDataPhys.CurrentCell.RowIndex
    txtRowIndex.text = rowDGVDataPhys
    DGVDataPhys.Rows(rowDGVDataPhys).Selected = True
    DGVData.Enabled = True
    DGVDataPhys.Enabled = True
    Dim CountRowIndexes As Integer = 0
    CountRowIndexes = DGVDataPhys.RowCount

'To capture how many total rows in the GRID.        
MsgBox(CountRowIndexes) 'I found 84 rows 
        vDeptID = DGVDataPhys.Rows(DGVDataPhys.SelectedCells(rowDGVDataPhys).RowIndex.ToString()).Cells("DeptKey").Value
        vDeptName = DGVDataPhys.Rows(DGVDataPhys.SelectedCells(rowDGVDataPhys).RowIndex.ToString()).Cells("DeptName").Value

1 个答案:

答案 0 :(得分:0)

您正在使用SelectedCells集合索引行,而不是当前单元格行索引...

改为

    vDeptID = DGVDataPhys.Rows(rowDGVDataPhys).Cells("DeptKey").Value
    vDeptName = DGVDataPhys.Rows(rowDGVDataPhys).Cells("DeptName").Value