参数超出范围异常有待解决

本文关键字:解决 异常 范围 参数 | 更新日期: 2023-09-27 18:30:17

我背后有一些代码,当我运行它时,弹出了一个异常。我想解决这个问题。它一直在吞噬我的思想。任何帮助,不胜感激。

    private void StudentListView_DoubleClick(object sender, EventArgs e)
    {
        ListViewItem selectedListViewCell=StudentListView.SelectedItems[0];//
        //problem is about the line above. I have an argument out of range exception  here.
        //it says that InvalidArgument=Value of '0' is not valid for 'index'.
        selectedStudent = (Student)selectedListViewCell.Tag;
        SetDataInTextBoxes();
        selectedRowIndex=StudentListView.SelectedIndices[0];
        SaveButton.Visible = false;
        CancelButton.Visible = false;
        UpdateButton.Visible = false;
    }

参数超出范围异常有待解决

这意味着列表视图中没有任何选定的项目。确保首先在设计器中选择了任何项。为避免异常,您应该首先检查然后执行此操作

ListViewItem selectedListViewCell;
if(StudentListView.SelectedItems.Count > 0)
     selectedListViewCell=StudentListView.SelectedItems[0];