争论超出范围异常待解决

时间:2013-09-05 07:26:23

标签: c# exception

我有一些代码,当我运行它时,会弹出一个异常。 我想解决这个问题。它一直在吃我的脑海。任何帮助表示赞赏。

    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;
    }

1 个答案:

答案 0 :(得分:1)

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

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