2015-09-27 72 views
0

我正在尝试在我的页面上显示网格,并且每次网格更改中的名称都显示该名称的Total。但是,我不断收到一个错误Object not set to instance of an object这是我的语法,我正在尝试,有人可以看看,并填补我在做什么错了吗?显示网格中的总行数每次更改名称

<asp:GridView ID="gvTest" runat="server" OnDataBound = "gvODB" OnRowCreated = "gvORC" > 
<Columns> 
    <asp:BoundField DataField="" HeaderText="userID"></asp:BoundField> 
    <asp:BoundField DataField="employeename" HeaderText="Name"></asp:BoundField> 
    <asp:BoundField DataField="hoursworked" HeaderText="Daily Hours"></asp:BoundField> 
</Columns> 
</asp:GridView> 


private int currentId = 0; 
private decimal subTotal = 0; 
private decimal total = 0; 
private int subTotalRowIndex = 0; 

protected void gvORC(object sender, GridViewRowEventArgs e) 
{ 
    DataTable dt = new DataTable(); 
    subTotal = 0; 
    try 
    { 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     dt = (e.Row.DataItem as DataRowView).DataView.Table; 
     int userID = Convert.ToInt32(dt.Rows[e.Row.RowIndex]["userID"]); 
     total += Convert.ToDecimal(dt.Rows[e.Row.RowIndex]["hoursworked"]); 
     if (userID != currentId) 
     { 
      if (e.Row.RowIndex > 0) 
      { 
       for (int i = subTotalRowIndex; i < e.Row.RowIndex; i++) 
       { 
        subTotal += Convert.ToDecimal(gvTest.Rows[i].Cells[2].Text); 
       } 
       this.AddTotalRow("Total", subTotal.ToString("N2")); 
       subTotalRowIndex = e.Row.RowIndex; 
      } 
      currentId = userID; 
     } 
    } 
    } 
    catch (Exception exception) 
    { 
    throw exception; 
    } 
} 
protected void AddTotalRow(string labelText, string value) 
{ 
    GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal); 
    row.BackColor = ColorTranslator.FromHtml("#F9F9F9"); 
    row.Cells.AddRange(new TableCell[3] { new TableCell(), //Empty Cell 
          new TableCell { Text = labelText,  HorizontalAlign = HorizontalAlign.Right}, 
          new TableCell { Text = value, HorizontalAlign = HorizontalAlign.Right } }); 

    gvTest.Controls[0].Controls.Add(row); 

} 

编辑 - 网格将返回一个用户名,用户名和工作小时数。我希望为每个返回的个人用户名显示总计。像这样 用户ID名字小时 1646红16 1646红8 共有24 1812蓝6 1812蓝8 共14

现在一个用户ID,姓名和时间总是会回来,唯一的一次空将所有结果添加到网格时返回值。

+0

没有人可以用你提供的信息写出一个好的答案,但是一般而言,你的Exception几乎可以肯定是你通过索引访问一个项目的一个或多个地方,然后在不进行任何空检查的情况下访问一个属性。另外,你是否知道页脚模板? – Crowcoder

+0

这个问题缺乏上下文让我确切地知道“每当网格中的名称发生变化”的含义。也许页脚模板在这里不合适。为了检查null,只需简单地测试'if(x == null)'。因此,例如,检查gvTest是否为空,然后测试gvTest.Rows是否为空,然后测试gvTest.Rows [I]是否为空,然后测试gvTest.Rows [I] .Cells是否为null等。 – Crowcoder

+0

@Crowcoder - 看我的编辑,这有助于澄清? – user5382234

回答

0

这个想法是生成将显示您的简单“报告”的HTML。有许多方法可以做到这一点。如果数据是只读的,然后使用网格不仅仅是建立一个table或设置的div,等等。你可以看看到中继器或ListView但是在这里我展示一个简单的table元素不太合适:

的ASPX标记是有一个简单的ASP:表与RUNAT服务器:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %> 

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server"> 

<asp:Table runat="server" ID="usertbl" border="1"> 

</asp:Table> 

</asp:Content> 

后面的代码:

using System; 
using System.Collections.Generic; 
using System.Data; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace WebApplication1 
{ 
    public partial class _Default : Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      //Contrived Data table just because I need some data to demonstrate. 
      //I assume you are or can acquire a DataTable with your current solution. 
      DataTable dt = new DataTable(); 
      dt.Columns.Add(new DataColumn("UserID", typeof(int))); 
      dt.Columns.Add(new DataColumn("UserName", typeof(string))); 
      dt.Columns.Add(new DataColumn("Hours", typeof(int))); 

      DataRow row = dt.NewRow(); 
      row[0] = 1646; 
      row[1] = "Red"; 
      row[2] = 16; 
      dt.Rows.Add(row); 

      row = dt.NewRow(); 
      row[0] = 1646; 
      row[1] = "Red"; 
      row[2] = 8; 
      dt.Rows.Add(row); 

      row = dt.NewRow(); 
      row[0] = 1812; 
      row[1] = "Blue"; 
      row[2] = 6; 
      dt.Rows.Add(row); 

      row = dt.NewRow(); 
      row[0] = 1812; 
      row[1] = "Blue"; 
      row[2] = 14; 
      dt.Rows.Add(row); 

      //Simplify accumualtor logic by grouping on Userid 
      var users = dt.AsEnumerable().GroupBy(u => u[0]); 

      //For each group (each user id) 
      foreach (var item in users) 
      { 
       //adds the user hours 
       int accumulator = 0; 

       //For each set or rows in group 
       foreach (var dr in item) 
       { 
        accumulator += int.Parse(dr[2].ToString()); 

        //Create a table row 
        TableRow tr = new TableRow(); 

        //Add the cells to the table row 
        TableCell userIdCell = new TableCell(); 
        userIdCell.Text = item.Key.ToString(); 
        tr.Cells.Add(userIdCell); 

        TableCell userNameCell = new TableCell(); 
        userNameCell.Text = dr[1].ToString(); 
        tr.Cells.Add(userNameCell); 

        TableCell hoursCell = new TableCell(); 
        hoursCell.Text = dr[2].ToString(); 
        tr.Cells.Add(hoursCell); 

        //Add the row to the table 
        usertbl.Rows.Add(tr); 
       } 

       //create summary row 
       TableRow totalRow = new TableRow(); 
       //Skip a cells 
       totalRow.Cells.Add(new TableCell()); 
       totalRow.Cells.Add(new TableCell()); 
       //total cell 
       TableCell userTotalCell = new TableCell(); 
       userTotalCell.Text = accumulator.ToString(); 
       totalRow.Cells.Add(userTotalCell); 

       //Finally add the row 
       usertbl.Rows.Add(totalRow); 
      } 
     } 
    } 
} 

最后结果: enter image description here

+0

哦,看起来很完美!他们是否可以将“Total”一词添加到实际的总行数中?如果没有,这是好的,但视觉上会很好。是的,数据是只读的。 – user5382234

+0

是的,你可以在其中一个空单元格中添加“总计”或任何你想要的。 – Crowcoder