C# 无法选择行;索引超出范围
我的应用程序有问题,我试图在新页面中使用与我的应用程序的其他部分相同的代码,但我不断收到错误。这就是实际情况:我有一个gridview,其中“自动生成选择按钮”设置为“true”,现在当您单击特定行的选择按钮时,将执行以下代码:C# 无法选择行;索引超出范围,c#,asp.net,C#,Asp.net,我的应用程序有问题,我试图在新页面中使用与我的应用程序的其他部分相同的代码,但我不断收到错误。这就是实际情况:我有一个gridview,其中“自动生成选择按钮”设置为“true”,现在当您单击特定行的选择按钮时,将执行以下代码: private void DisplayDetails() { int rowIndex = dtGridViewList.SelectedIndex; GridViewRow Row = dtGridViewList.Selec
private void DisplayDetails()
{
int rowIndex = dtGridViewList.SelectedIndex;
GridViewRow Row = dtGridViewList.SelectedRow;
cmbCompany.SelectedValue = dtGridViewList.DataKeys[rowIndex].Values["CompanyID"].ToString();
cmbDept.SelectedValue = dtGridViewList.DataKeys[rowIndex].Values["DeptID"].ToString();
txtProject.Text = Row.Cells[1].Text;
cmbCompany.SelectedValue = Row.SelectedRow.Cells[2].Text;
cmbDept.SelectedValue = Row.Cells[3].Text;
txtStartDate.Text = Row.Cells[4].Text;
txtEndDate.Text = Row.Cells[5].Text;
cmbManager.SelectedValue = Row.Cells[6].Text;
cmbLocation.SelectedValue = Row.Cells[7].Text;
txtNotes.Text = Row.Cells[6].Text;
}
此函数在GridView“SelectedIndexChanged”事件中执行:
错误如下:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
我在其他页面上使用了相同的代码,但它运行得非常好,所以我无法理解为什么它不适用于他的页面。好的,非常感谢大家的贡献,但我成功地解决了这个问题。 这是我以前做的:
这就是我为解决问题所做的,我想了解一下我为什么会出现这个问题。您是否确认dtGridViewList在调用它时包含数据,即它是作为回发的一部分调用的,还是在页面加载期间运行的?是的,数据显示在我和f的正前方填充gridview的函数是在页面加载时运行的,而不是在回发期间。您可以显示更多的代码,特别是SelectedIndexChanged方法中的内容吗?是否值得使用
SelectedIndexChanged
和/或SelectedIndexChanged
方法来设置所选行?我已经进行了编辑,添加了我的行中的内容selectedIndexChanged方法。
GridViewRow Row = dtGridViewList.SelectedRow;
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index