繁体   English   中英

修复数据绑定:“ System.Data.DataRowView”不包含名称为“

[英]Fix- DataBinding: 'System.Data.DataRowView' does not contain a property with the name

create procedure St_Proc_GetUserReportforCurrentDayTask                  
@userID int                  
as                  
    Begin                  
        set NoCount on;                  
        DECLARE @TODAY DATE                    
        SET @TODAY = CONVERT(VARCHAR(10), GETDATE(), 111)                  
        select  CONVERT(VARCHAR,production.CalendarDate,101) + RIGHT (CONVERT(VARCHAR,production.CalendarDate , 100 ) ,7) as Date,                   
        RegionAndProjectInfo.RegionProjectName as Region ,                  
        County.CountyName as County,                  
        WorkType.WorkTypeName as WorkType,                  
        Task.TaskName as Task,      
        Production.VolumeProcessed as 'Volumes Processed',                  
        Production.TimeSpent as 'Duration(HH:MM)'                  
        from Production                   
        inner join RegionAndProjectInfo                  
        on                  
        RegionAndProjectInfo.RegionProjectID=Production.RegionProjectID                  
        inner join County                  
        on                   
        County.CountyID=Production.CountyID                  
        inner join WorkType                  
        on                  
        WorkType.WorkTypeID=Production.WorkTypeID                  
        inner join Task                  
        on                  
        Task.TaskID=Production.TaskID                  
        where Production.UserID=@userID and CalendarDate >= @TODAY                  
    End 

这是我的存储过程,我正在执行此存储过程,并将结果保存在数据集中,然后将网格与该数据集绑定。

private void BindGridOnPageLoad()
    {
        _production = new EmployeeQuotientCL.Production();
        _userEcode = Convert.ToString(Session["UserID"]);
        int _userID = _production.GetUserIDFromDB(_userEcode);
        dsUserTaskDetails = _production.GetTabularUsertaskDetails(_userID);
        BindGridView(dsUserTaskDetails);
    }

到这里一切都很好。然后我要求在网格视图的页脚中显示Duration(HH:MM)total。我在页面中添加了以下代码:-

<div id="gridview">
            <asp:GridView ID="GVUserEnteredDetails" runat="server" ShowFooter="true" CssClass="mGrid"  CellSpacing="2"   onselectedindexchanged="GVUserEnteredDetails_SelectedIndexChanged" onrowdatabound="GridView1_RowDataBound">                  
            </asp:GridView>

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            tp += Convert.ToDecimal(((DataRowView)e.Row.DataItem).Row["[Duration(HH:MM)]"]);
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[0].Text = "Total Hours : ";
            e.Row.Cells[1].Text = tp.ToString("c");
            e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Font.Bold = true;

        }
    }

但是,当我调试即时消息时,出现此错误:-“ DataBinding:'System.Data.DataRowView'不包含名称为'Duration'的属性。 我在做什么错误,以及如何解决? 任何建议和帮助都会对我有所帮助。

堆栈跟踪:-

[ArgumentException:列'[Duration(HH:MM)]'不属于表Table。] System.Data.DataRow.GetDataColumn(String columnName)+2124943 System.Data.DataRow.get_Item(String columnName)+13 EQ。 C:\\ Users \\ dsingh \\ Documents \\ Visual Studio 2010 \\ Projects \\ EQ \\ EQ \\ Associates \\ Production.aspx.cs:112 System.Web.UI.WebControls.GridView中的Associates.Production.GridView1_RowDataBound(Object sender,GridViewRowEventArgs e) .OnRowDataBound(GridViewRowEventArgs e)+115 System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex,Int32 dataSourceIndex,DataControlRowType rowType,DataControlRowState rowState,Boolean dataBind,Object dataItem,DataControlField []字段,TableRowCollection行,PagedDataSource pagedDataSource) 181 System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource,Boolean dataBinding)+3896 System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data)+66 System.Web.UI.WebControls.GridView.PerformDataBinding( IE大量数据)+14 S ystem.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data)+128 System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments,DataSourceViewSelectCallback callback)+33 System.Web.UI.WebControls.DataBoundControl.PerformSelect()+143 System.Web.UI.WebControls.BaseDataBoundControl.DataBind()+74 System.Web.UI.WebControls.GridView.DataBind()+4 EQ.Associates.Production.BindGridView(DataSet ds)在C:\\ Users \\ dsingh \\ Documents中\\ Visual Studio 2010 \\ Projects \\ EQ \\ EQ \\ Associates \\ Production.aspx.cs:603 C:\\ Users \\ dsingh \\ Documents \\ Visual Studio 2010 \\ Projects \\ EQ \\ EQ \\ Associates中的EQ.Associates.Production.BindGridOnPageLoad() \\ Production.aspx.cs:593 C:\\ Users \\ dsingh \\ Documents \\ Visual Studio 2010 \\ Projects \\ EQ \\ EQ \\ Associates \\ Production.aspx.cs中的EQ.Associates.Production.Page_Load(Object sender,EventArgs e): 78 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp,Object o,Object t,EventArgs e)+14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object se nder,EventArgs e)+35 System.Web.UI.Control.OnLoad(EventArgs e)+91 System.Web.UI.Control.LoadRecursive()+74 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint )+2207

正如您在评论中提到的那样,在以HH:mm表示的数据的十进制解析中遇到了麻烦

您应该使用TimeSpan对所有这些值求和,而不是十进制。 TimeSpan具有从4.0开始解析的确切功能,但是如果您使用的是次要版本)

尝试以这种方式进行

TimeSpan ts = DateTime.ParseExact("02:30", "HH:mm", CultureInfo.InvariantCulture).TimeOfDay;
// instead of 02:30 you would have the string from your column passed in

// you can add up the values in this fashion
ts.Add(DateTime.ParseExact("08:30", "HH:mm", CultureInfo.InvariantCulture).TimeOfDay);

您可以使用Resultant Timespan来显示您选择的格式,因为它也会有天数,或者您始终可以显示ts.TotalMinutes

希望这是您正在寻找的东西,请更新相关部分

请尝试在列名周围使用方括号,因为它包含特殊字符。 喜欢,

 tp += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "[Duration(HH:MM)]"));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM