使用以下代码行,我可以打印行的主键:
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
答案 0 :(得分:1)
尝试查看此帖子How do I get the value of a gridview Cell?,注意它只会尝试检索类型为DataControlRowType.DataRow
的行的值。尝试从标题行中获取不存在的"PF_NonScrumStory"
值可能会导致您的空引用异常。
答案 1 :(得分:0)
您需要在对象上调用ToString()
,但可能会发生NullReferenceException
,因为该对象为空。当您调试并在异常中停止时,请查看本地(Debug - > Windows - > Locals)以查看其实际值。
答案 2 :(得分:0)
尝试显式转换为(int)以摆脱“装箱”,然后调用.ToString()