I have a simple datagrid, that displays contents of some List variable. I have two buttons related to it, one for adding contents to the List variable and refreshing the datagrid, the other for removing it and refreshing also.
然而,如果我改变所选择的行文,我就犯了一个错误,说“Index 1没有价值” 没有任何理由。
建立数据网的代码如下:
allTravellersDataGrid.DataSource = allTravellers;
名单上所有流浪者都在上面谈论。
将内容列入所有游轮名单的序号如下:
private void addAttendee_Click(object sender, EventArgs e)
{
if (attendeeName.Text == "" || attendeeSurname.Text == "" || attendeeBirthDate.Text == "" || attendeeIdNumber.Text == "")
{ MessageBox.Show("Not all information regarding the attendee entered"); }
else
{
allTravellers.Add(dt.prepareTraveller(attendeeName.Text, attendeeSurname.Text, attendeeBirthDate.Text, attendeeIdNumber.Text));
allTravellersDataGrid.DataSource = null;
allTravellersDataGrid.DataSource = allTravellers;
allTravellersDataGrid.Refresh();
}
}
and at last the code for button for removal of objects from the list looks like this:
private void removeAttendee_Click(object sender, EventArgs e)
{
traveller travellerToRemove = (traveller)allTravellersDataGrid.CurrentRow.DataBoundItem;
allTravellers.Remove(travellerToRemove);
allTravellersDataGrid.Refresh();
}
是否有任何人像我这样无助地发现什么造成指数问题?
感谢。