如何在GridView ASP.NET C#4.0的最后一列中添加列值并显示

时间:2015-01-05 11:01:34

标签: c# asp.net gridview

Heres My Code

Timesheet.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Timesheet.aspx.cs" Inherits="Timesheet" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true" OnRowDataBound="OnRowDataBound" PageIndex="0" PageSize="20">
        </asp:GridView>
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>

Timesheet.aspx.cs

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using TimesheetBLL;
public partial class Timesheet : System.Web.UI.Page
{
    clsTimesheetBLL bll = new clsTimesheetBLL();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }
    protected void BindData()
    {
        BoundField bfield = new BoundField();
        bfield.HeaderText = "Rig / Task";
        bfield.DataField = "rigtask";
        GridView1.Columns.Add(bfield);

        DataTable dt2 = bll.GetEmp();
        if (dt2.Rows.Count > 0)
        {
            for (int i = 0; i < dt2.Rows.Count; i++)
            {
                TemplateField tfield = new TemplateField();
                tfield.HeaderText = dt2.Rows[i]["name"].ToString();
                GridView1.Columns.Add(tfield);
            }

        }

        TemplateField tfield2 = new TemplateField();
        tfield2.HeaderText = "Total";
        GridView1.Columns.Add(tfield2);

        DataTable dt = bll.GetRigTask();
        if (dt.Rows.Count > 0)
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }`enter code here`

    }

    protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataTable dt2 = bll.GetEmp();
            if (dt2.Rows.Count > 0)
            {
                for (int i = 1; i <= dt2.Rows.Count; i++)
                {
                    TextBox txtCountry = new TextBox();
                    txtCountry.ID = "txtCountry";
                    e.Row.Cells[i].Controls.Add(txtCountry);
                }

                Label lblTotal = new Label();
                lblTotal.ID = "lblTotal";
                e.Row.Cells[dt2.Rows.Count + 1].Controls.Add(lblTotal);
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        var total = 0;
        foreach (GridViewRow row in GridView1.Rows)
        {
                TextBox txtTotal = new TextBox();
                txtTotal = (TextBox)row.FindControl("txtCountry");
                total += Convert.ToInt32(txtTotal.Text);
        }
    }
}

所有列和行都是动态生成的,并且使用两个不同的表请帮助我,如果用户输入其小时,则显示最后一列中的总数(总计)[项目总计小时数]。 并为每位员工提供页脚。 如果有人知道如何将其存储在数据库中,那将会很有帮助。

0 个答案:

没有答案