C# 索引超出范围。必须为非负数且小于集合的大小。尝试在gridview中填充下拉列表

C# 索引超出范围。必须为非负数且小于集合的大小。尝试在gridview中填充下拉列表,c#,asp.net,gridview,C#,Asp.net,Gridview,我有以下GridView(我已删除(剪切)此过程不需要的字段): 问题是,它在线路上出现故障: GridViewRow row = DataGrid_ResourceAllocation.Rows[e.NewEditIndex]; 错误页面将显示: 索引超出范围。必须为非负数且小于 收藏。参数名称:索引 描述:一个未处理的 执行当前web请求期间发生异常。 请查看堆栈跟踪以了解有关错误和错误的更多信息 它起源于代码的地方 异常详细信息:System.ArgumentOutOfRange异常:索

我有以下GridView(我已删除(剪切)此过程不需要的字段):

问题是,它在线路上出现故障:

GridViewRow row = DataGrid_ResourceAllocation.Rows[e.NewEditIndex];
错误页面将显示:

索引超出范围。必须为非负数且小于 收藏。参数名称:索引

描述:一个未处理的 执行当前web请求期间发生异常。 请查看堆栈跟踪以了解有关错误和错误的更多信息 它起源于代码的地方

异常详细信息:System.ArgumentOutOfRange异常:索引已过期 射程。必须为非负数且小于 收集参数名称:索引

源错误:

第195行:SqlConnection conn=GetConnection();第196行: 第197行:GridViewRow行= DataGrid_ResourceAllocation.Rows[e.NewEditIndex];第198行:
DropDownList ddList=row.FindControl(“ddl\u项目\u所有者”)作为 下拉列表;第199行:

源文件: C:\Users\C35392\Documents\Work\Ad Hoc\Bob\ResourceAllocation\ResourceAllocation\ResourceAllocation.aspx.cs 电话号码:197

堆栈跟踪:

[ArgumentOutOfRangeException:索引超出范围。必须为 非负且小于集合的大小。参数名称: 索引]System.Collections.ArrayList.get_项(Int32索引)+12547654 System.Web.UI.WebControl.GridViewRowCollection.get_项(Int32索引) +15 RAT.ResourceAllocation.DataGrid\u ResourceAllocation\u EditCommand(对象 发件人,GridViewEditEventArgs)位于 C:\Users\C35392\Documents\Work\Ad Hoc\Bob\ResourceAllocation\ResourceAllocation\ResourceAllocation.aspx.cs:197 System.Web.UI.WebControl.GridView.OnRowEditing(GridViewEditEventArgs e) +122 System.Web.UI.WebControl.GridView.HandleEdit(Int32 行索引)+43
System.Web.UI.WebControl.GridView.HandleEvent(EventArgs e,布尔值 原因验证,字符串验证组)+715
System.Web.UI.WebControl.GridView.OnBubbleEvent(对象源, EventArgs e)+89 System.Web.UI.Control.RaiseBubbleEvent(对象 来源,事件args args)+37
System.Web.UI.WebControl.GridViewRow.OnBubbleEvent(对象源, EventArgs e)+90 System.Web.UI.Control.RaiseBubbleEvent(对象 来源,事件args args)+37
System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e)+114
System.Web.UI.WebControl.Button.RaisePostBackEvent(字符串 eventArgument)+260
System.Web.UI.WebControl.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(字符串 eventArgument)+12
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,String eventArgument)+15
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35 System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔IncludeStagesSafterAsyncPoint) +1639年


谁能告诉我我做错了什么吗?

我想问题出在这方面

<asp:DropDownList ID="ddl_Project_Owner" runat="server" Text='<%#Eval("Project_Owner") %>'></asp:DropDownList>

通过设置
Text
属性,站点希望将所选值设置为
Project\u Owner
。因此,如果DDL为空或该值不存在,则会出现该错误


最好在代码隐藏中设置SelectedValue并删除
Text='

发生这种情况时
e.NewEditIndex
的值是多少?DataGrid\u ResourceAllocation.Rows的计数是多少?我插入了这个:int RAC=DataGrid\u ResourceAllocation.Rows.count;它返回0。e、 NewEditIndex是我单击编辑按钮的行号,假设它是基于零的。例如,我点击了第三行的编辑按钮,e.NewEditIndex返回了2。这不是你的问题吗?如果
DataGrid\u ResourceAllocation.Rows.Count
为0,而
e.NewEditIndex
为2,那么当您尝试访问
DataGrid\u ResourceAllocation.Rows[e.NewEditIndex]
时,您预计会发生什么情况?@ChrisDunaway-但为什么会是0?在我单击编辑按钮之前,gridview中有记录。我看不到您在哪里显示网格是如何填充数据的。我猜您是在客户端而不是服务器端进行填充,因此下面的代码认为没有行。假设我是一个白痴(我认为这是一个非常公平的假设),我将如何以及在何处执行此操作?您正在使用
DataGrid\u ResourceAllocation\u EditCommand
方法将数据绑定到该DDL。紧跟在
1ddList.DataBind()之后我会是一个好地方。这很有意义,因为我最初是从文本框中获取代码的。然而,这并没有解决问题。
GridViewRow row = DataGrid_ResourceAllocation.Rows[e.NewEditIndex];
<asp:DropDownList ID="ddl_Project_Owner" runat="server" Text='<%#Eval("Project_Owner") %>'></asp:DropDownList>