我正在尝试在我的页面上显示网格,每次网格中的名称更改都会显示该名称的总计。但是,我一直收到错误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 :(得分:0)
想法是生成将显示简单“报告”的html。有很多方法可以做到这一点。如果只读取数据,那么使用网格不仅仅是构建table
或一组div等等。您可以查看Repeater或ListView,但在这里我演示了一个简单的table
元素:
具有简单asp的aspx标记:带有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);
}
}
}
}