0
我遇到的问题与我的dataGridView导致单击视图的列标题时出现EventOutOfRange异常错误。我明白为什么发生错误,我只是在创建工作时遇到问题。有没有办法可以创建一个if语句来查看所选索引是否为-1?我所尝试过的一切都是无效的。SelectedIndexChanged EventOutOfRange在C#中单击datagridview标题时出现异常
private void student_grid_SelectionChanged(object sender, EventArgs e)
{
DataGridView theGrid = sender as DataGridView;
if (theGrid != null)
{
//The line below is what causes the error.
DataGridViewRow row = theGrid.SelectedRows[0];
stuinfo_fill.Text = row.Cells["Student ID"].Value.ToString();
}
if (stuinfo_fill.Text == String.Empty)
{
MessageBox.Show("You must have a student selected.");
}
else
{
PhysicianFill(stuinfo_fill.Text);
StudentInfo(stuinfo_fill.Text);
sch_rad.Checked = false;
newsch_btn.Enabled = true;
schList_btn.Enabled = true;
phynew_btn.Enabled = true;
phylist_btn.Enabled = true;
}
}
_if(theGrid!= NULL && theGrid.SelectedRows!= NULL)_ – Steve
两个条件都满足,错误仍然发生。有没有办法做&& theGrid.SelectedRows!= -1)。我这样做的时候会出错。 –
在_if(.. && theGrid.SelectedRows.Count> 0)上添加一个检查_ – Steve