0

使用以下代码行,我可以打印行的主键:

protected void storyGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
    System.Diagnostics.Debug.WriteLine(DataBinder.Eval(e.Row.DataItem, "PK_NonScrumStory"));
}

在此处输入图像描述

但是我不能将值分配给字符串变量:

在此处输入图像描述在此处输入图像描述

如何获取分配给变量的值?

编辑:

堆栈跟踪:

[NullReferenceException: Object reference not set to an instance of an object.]
   user_nonscrumstories2.storyGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e) in c:\Users\tunnelld2\Documents\Visual Studio 2012\Websites\ZSA_New 21\user\nonscrumstories2.aspx.cs:424
   System.Web.UI.WebControls.GridView.OnRowDataBound(GridViewRowEventArgs e) +111
   System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) +181
   System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +2188
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +67
   System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +14
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +123
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +33
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +138
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +30
   System.Web.UI.WebControls.GridView.DataBind() +4
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +105
   System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +75
   System.Web.UI.Control.EnsureChildControls() +83
   System.Web.UI.Control.PreRenderRecursiveInternal() +42
   System.Web.UI.Control.PreRenderRecursiveInternal() +168
   System.Web.UI.Control.PreRenderRecursiveInternal() +168
   System.Web.UI.Control.PreRenderRecursiveInternal() +168
   System.Web.UI.Control.PreRenderRecursiveInternal() +168
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +974
4

3 回答 3

1

尝试看一下这篇文章如何获得 gridview Cell 的值?,请注意它如何只尝试检索类型为 的行的值DataControlRowType.DataRow。尝试"PF_NonScrumStory"从标题行获取不存在的值可能会导致您的空引用异常。

于 2013-11-08T18:49:50.780 回答
0

您确实需要调用ToString()该对象,但 aNullReferenceException可能正在发生,因为该对象为空。当您调试并在异常时停止时,请查看局部变量(调试 -> Windows -> 局部变量)以查看其实际值。

于 2013-11-08T18:44:55.890 回答
0

尝试显式转换为 (int) 以摆脱“装箱”,然后调用 .ToString()

于 2013-11-08T18:47:42.423 回答