vb.net 中 datagridview 中的索引超出范围异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19811292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:44:00  来源:igfitidea点击:

index out of range exception in datagridview in vb.net

vb.netwinformsdatagridview

提问by vimal vasudevan

following is the code which i have used while selecting the particular rows column value on pressing the F9 key.but i get the error as argument out of range exception was handled.detailed error comes as index out of range exception.

以下是我在按 F9 键时选择特定行列值时使用的代码。但是我收到错误,因为参数超出范围异常被处理。详细错误是索引超出范围异常。

Private Sub dgsearchitemlist_KeyDown(ByVal sender As Object, _
       ByVal e As System.Windows.Forms.KeyEventArgs) _
       Handles dgsearchitemlist.KeyDown

    If e.KeyCode = Keys.F9 Then
        itemcode = dgsearchitemlist.SelectedRows(0).Cells(0).Value

        description = dgsearchitemlist.SelectedRows(0).Cells(2).Value.ToString
        uom = dgsearchitemlist.SelectedRows(0).Cells(3).Value.ToString

    End If
End Sub

回答by Wasif Hossain

Alternative 1:

备选方案 1:

Perhaps dgsearchitemlist.SelectionMode is NOT SET to either RowHeaderSelector FullRowSelect. Manually selecting all the cells of a row does not select that row. please check and set the property to any of these values.

也许 dgsearchitemlist.SelectionMode 未设置为RowHeaderSelectFullRowSelect手动选择一行的所有单元格不会选择该行。请检查并将属性设置为这些值中的任何一个。

Alternative 2:

备选方案 2:

If you need just the last selected row, then you can use dgsearchitemlist.CurrentRowinstead of dgsearchitemlist.SelectedRows(0). Then you don't have to check whether any rows have been selected or not.

如果您只需要最后选择的行,那么您可以使用dgsearchitemlist.CurrentRow而不是 dgsearchitemlist.SelectedRows(0)。然后您不必检查是否已选择任何行。

Hope any of these alternatives will click !

希望这些替代品中的任何一个都会点击!

回答by Edper

Most likely you reference a column index that does not exist, in this case it could be either of this code:

您很可能引用了一个不存在的列索引,在这种情况下,它可能是以下代码之一:

  description = dgsearchitemlist.SelectedRows(0).Cells(2).Value.ToString
  uom = dgsearchitemlist.SelectedRows(0).Cells(3).Value.ToString

That is Cells(2) or Cell(3). If you have only two columns and you have index of 2 that means you are accessing column 3 and if you have an index 3 it means you are accessing column 4. And any of those columns does not exist then it would be index out of range.

即 Cells(2) 或 Cell(3)。如果您只有两列并且您的索引为 2,这意味着您正在访问第 3 列,如果您有一个索引 3,则意味着您正在访问第 4 列。而这些列中的任何一个都不存在,那么它将是索引超出范围.

回答by ??ssa P?ngj?rdenlarp

It sounds like maybe in some cases, there are no rows selected. try this:

听起来可能在某些情况下,没有选择任何行。尝试这个:

Private Sub dgsearchitemlist_KeyDown(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.KeyEventArgs) _
    Handles dgsearchitemlist.KeyDown

    If dgsearchitemlist.SelectedRows.Count = 0  Then Exit Sub

    If e.KeyCode = Keys.F9 Then
        itemcode = dgsearchitemlist.SelectedRows(0).Cells(0).Value
        ...