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