【问题标题】:Fix- DataBinding: 'System.Data.DataRowView' does not contain a property with the name修复- DataBinding:“System.Data.DataRowView”不包含名称为的属性
【发布时间】:2012-07-13 10:41:41
【问题描述】:
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) 总计。我在页面中添加了以下代码:-

<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 C:\Users\dsingh\Documents\Visual Studio 2010\Projects\EQ\EQ\Associates\Production.aspx.cs:112 中的 EQ.Associates.Production.GridView1_RowDataBound(Object sender, GridViewRowEventArgs e) System.Web.UI.WebControls.GridView.OnRowDataBound(GridViewRowEventArgs e) +115 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) +3896 System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable 数据)+66 System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable 数据)+14 System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable 数据)+128 System.Web.UI.DataSourceView.Select(DataSourceSelectArguments 参数,DataSourceViewSelectCallback 回调)+33 System.Web.UI.WebControls.DataBoundControl.PerformSelect() +143 System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74 System.Web.UI.WebControls.GridView.DataBind() +4 C:\Users\dsingh\Documents\Visual Studio 2010\Projects\EQ\EQ\Associates\Production.aspx.cs:603 中的 EQ.Associates.Production.BindGridView(DataSet ds) C:\Users\dsingh\Documents\Visual Studio 2010\Projects\EQ\EQ\Associates\Production.aspx.cs:593 中的 EQ.Associates.Production.BindGridOnPageLoad() C:\Users\dsingh\Documents\Visual Studio 2010\Projects\EQ\EQ\Associates\Production.aspx.cs:78 中的 EQ.Associates.Production.Page_Load(Object sender, EventArgs e) System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp,对象 o,对象 t,EventArgs e)+14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(对象发送者,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

【问题讨论】:

  • 您是否将正确的表绑定到GridView
  • 是的,我在我的问题中指定,直到数据绑定和只显示网格,每件事都工作正常,但是当我试图添加页脚时,我得到了这个错误。
  • 有点奇怪,你的DataSource显示Duration...是什么(希望过程和上面你执行的一样)
  • 是的,我使用相同的程序,在 Datagrid 中,列标题名称显示为:日期、地区、县、工作类型、任务、处理的卷数、持续时间(HH:MM)
  • 那么您所做的唯一更改就是添加RowDataBound 事件?

标签: c# asp.net


【解决方案1】:

正如您在 cmets 中提到的,您在对您表示为 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

希望这是您所寻找的,否则请更新相关部分

【讨论】:

  • 是的,实际上这是我正在寻找的。现在我可以在页脚中填充总数。非常感谢
【解决方案2】:

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

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

【讨论】:

  • 按照您的要求完成,但这我得到了这个异常“异常已被调用的目标抛出。”
  • 你能发布详细的异常堆栈跟踪吗?
  • 您甚至可以尝试:tp += Convert.ToDecimal(((DataRowView)e.Row.DataItem).Row["[Duration(HH:MM)]")。但它假设您使用 Datatable 或 Dataset 作为数据源。
  • 更新了问题并在那里提到了堆栈跟踪。
  • .Net 似乎为您的数据列分配了一些不同的名称。只需检查它在调试模式下分配的名称并替换为上述语句中的名称。
猜你喜欢
  • 2011-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多