2015-09-27 36 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