如何使用CommandField编辑GridView中的条目

时间:2015-02-13 17:03:32

标签: c# asp.net

我的gridview中有以下命令字段:

<asp:CommandField ShowEditButton="True" HeaderText="EDIT" CausesValidation="false"
    HeaderStyle-BackColor="#CCCCCC" HeaderStyle-CssClass="infoBoldBlueSmall">
    <HeaderStyle BackColor="#CCCCCC"></HeaderStyle>
</asp:CommandField>

条目如下所示:

enter image description here

当我点击编辑按钮时,我看到更新取消:

enter image description here

对于某些单击编辑按钮的条目,我收到以下错误:

Server Error in '/' Application.
--------------------------------------------------------------------------------

'Ddl_c' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentOutOfRangeException: 'Ddl_c' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 


[ArgumentOutOfRangeException: 'Ddl_c' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value]
   System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable dataSource) +2732917
   System.Web.UI.WebControls.ListControl.PerformSelect() +49
   System.Web.UI.Control.DataBindChildren() +11143343
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +182
   System.Web.UI.Control.DataBindChildren() +11143343
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +182
   System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) +267
   System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +3340
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +72
   System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +18
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +147
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +261
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
   System.Web.UI.WebControls.GridView.OnPreRender(EventArgs e) +46
   System.Web.UI.Control.PreRenderRecursiveInternal() +108
   System.Web.UI.Control.PreRenderRecursiveInternal() +224
   System.Web.UI.Control.PreRenderRecursiveInternal() +224
   System.Web.UI.Control.PreRenderRecursiveInternal() +224
   System.Web.UI.Control.PreRenderRecursiveInternal() +224
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3394

--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.5485; ASP.NET Version:2.0.50727.5483 

如果需要更多信息,请告知我们。

填充了gridview,所以我知道数据在那里,我也检查了表格,它也在那里。

我该如何解决这个问题?

更新:

代码发布在这里:

http://pastebin.com/ezr9uNMc

显示GridView的图片:

enter image description here

每行点击Edit时会触发什么?如果我点击Edit获得52,它可以正常工作但是对于57,58和59我得到了堆栈跟踪错误。

请帮我解决问题。

2 个答案:

答案 0 :(得分:1)

获得代码会很有帮助,但我之前可能已遇到此问题。您可以在绑定行中的数据之前设置所选项目,因此您可能需要在RowDataBound事件中设置最初选择的项目。

如果没有这样做,请确保您处理的情况是您没有在列表中获取任何项目,因此您不想设置所选项目。

答案 1 :(得分:0)

问题必须是为每一行填充的下拉列表 单击编辑按钮时,下拉列表的选定值不存在 确保下拉列表项中存在绑定值。