乘以gridview列

时间:2011-01-16 15:05:07

标签: c# asp.net

我会发给你我的代码,我会解释我想做的事情

<div>
<div>
    <asp:GridView ID="gridView1" runat="server">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="chkSelect" runat="server" />
                    <asp:HiddenField ID="hdValue" runat="server" Value='<%#Eval("ID") %>' />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
</div>
<div>
    <asp:Button ID="btnMove" runat="server" Text="Add To Cart" OnClick="btnMove_Click" />
</div>
<div>

    <asp:GridView ID="gridView2" runat="server">
    <Columns>
    <asp:TemplateField HeaderText="Quantity">
        <ItemTemplate>
        <asp:TextBox ID="tbQty" runat="server" Width="25px"
        MaxLength="3" />
        </ItemTemplate>
        <ItemStyle Width="25px" HorizontalAlign="Center"/>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>

</div>

<br />

<asp:Button ID="Button1" runat="server" Text="Find the total" />
<asp:Label ID="Label7" runat="server" Text="Total"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged"></asp:TextBox>
<br />
<br />
</div>

和theo.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;

public partial class theo : System.Web.UI.Page
{
    const string key = "MyDataSource5";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridView();
        }

    }
    private void BindGridView()
    {
        if (Session[key] == null)
        {
            gridView1.DataSource = GetDataSource();
            gridView1.DataBind();
        }
        else
        {
            gridView1.DataSource = (DataTable)Session[key];
            gridView1.DataBind();
        }

    }
    protected DataTable GetDataSource()
    {
        try
        {
            DataTable dt = new DataTable();
            dt = new DataTable();
            dt.Columns.Add("ID", typeof(int)).AutoIncrement = true;
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Price(Grouch)/Hectares", typeof(float));
            DataColumn[] keys = new DataColumn[2];
            keys[0] = dt.Columns["ID"];
            dt.PrimaryKey = keys;
            dt.Rows.Add("1", "Seaside Location", 1.5);
            dt.Rows.Add("2", "Arable Land", 0.1);
            dt.Rows.Add("3", "Foothills of the mountains", 1.5);
            dt.Rows.Add("4", "Industrial Land", 0.1);
            dt.Rows.Add("5", "Rolling Farmland", 0.5);
            Session[key] = dt;
            return dt;

        }
        catch
        {
            return null;
        }
    }
    protected void btnMove_Click(object sender, EventArgs e)
    {
        try
        {
            DataTable dtMain = Session[key] as DataTable;
            //copy the schema of source table
            DataTable dtClone = dtMain.Clone();
            foreach (GridViewRow gv in gridView1.Rows)
            {
                CheckBox chk = gv.FindControl("chkSelect") as CheckBox;
                HiddenField hdValue = gv.FindControl("hdValue") as HiddenField;
                if (chk.Checked)
                {
                    //get only the rows you want
                    DataRow[] results = dtMain.Select("ID=" + hdValue.Value + "");
                    //populate new destination table
                    foreach (DataRow dr in results)
                    {
                        dtClone.ImportRow(dr);
                    }
                }
                gridView2.DataSource = dtClone;
                gridView2.DataBind();
            }
        }
        catch
        {
            BindGridView();
        }

    }
}

在此代码中,当我从gridview 1检查示例2个选项并单击按钮添加到购物车时,这两个选项将移动到第二个gridview。如您所见,我有一个数量的文本框。我希望当我给出数量乘以它的价格时。用“查找总数”按钮我想在文本框中给我结果。我该怎么做?

1 个答案:

答案 0 :(得分:0)

一个简单的解决方案可能对您有用。

protected void Button1_Click(object sender, EventArgs e)
{
    int itemCount;
    decimal itemPrice, itemTotal;
    itemTotal = 0;
    itemCount = 0;
    itemPrice = 0;
    foreach(GridViewRow gridView2Row in gridView2.Rows)
    {
        TextBox tbCount = gridView2Row.Cells[0].Controls[1] as TextBox;
        itemCount = Convert.ToInt32(tbCount.Text);
        itemPrice = Convert.ToDecimal(gridView2Row.Cells[3].Text);
        itemTotal = itemTotal + (itemCount * itemPrice);

    }
    TextBox1.Text = itemTotal.ToString();
}