如何在gridview asp.net中将两列相乘c#

时间:2016-06-02 11:22:54

标签: c# asp.net

我有gridview i我希望将4列乘以数量以获得总数我不知道如何做到这一点请帮助我,请。这是代码

<asp:GridView ID="GridView1" runat="server"  
                onrowdatabound="GridView1_RowDataBound" onselectedindexchanged="GridView1_SelectedIndexChanged" 
              >
                <Columns>
                    <asp:TemplateField HeaderText="Item Name">
                        <ItemTemplate>
                            <asp:DropDownList ID="cbGvProd" runat="server" Height="16px" Width="200px">
                            </asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Quantity">
                        <ItemTemplate>
                            <asp:TextBox ID="tbQty" runat="server"></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Rate">
                        <ItemTemplate>
                            <asp:TextBox ID="tbRate" runat="server"></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Amount">
                        <ItemTemplate>
                            <asp:TextBox ID="tbAmount" runat="server"></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

代码:

protected void Page_Load(object sender, EventArgs e)
    {
        SetGrid();
    }
    public void SetGrid()
    {

        DataTable tdTemp = new DataTable("TSno");
        DataColumn dc = new DataColumn("TDc");
        tdTemp.Columns.Add(dc);

        for (int a = 0; a <= 24; a++)
        {
            tdTemp.Rows.Add(a);
        }

        tdTemp.AcceptChanges();


        GridView1.DataSource = tdTemp;
        GridView1.DataBind();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            int qty = Convert.ToInt32(GridView1.Rows[i].Cells[1].ToString());
            int rate= Convert.ToInt32(GridView1.Rows[i].Cells[2].ToString());

            int total=qty * rate;
            GridView1.Rows[i].Cells[3].Text = total.ToString();

        }
    }

2 个答案:

答案 0 :(得分:2)

添加模板字段以获取总计,无需在RowDataBound

上进行计算
<asp:TemplateField HeaderText="Total">
    <ItemTemplate>
       <asp:Label ID="lblProductTotal" runat="server" 
        Text='<%# ((Convert.ToInt32(Eval("Quantity")))*(Convert.ToInt32(Eval("Price"))))%>'>
        </asp:Label>
    </ItemTemplate>
</asp:TemplateField>

QuantityPrice替换为您正在使用的列,表示来自数据集或数据表

希望这会对你有所帮助。

答案 1 :(得分:0)

下面是使用jQuery的示例。查看下面的代码并了解它的工作原理,然后对您的应用程序进行更改

代码背后:

public partial class GridTwo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        List<OrderLineItem> lineItems = new List<OrderLineItem>();
        lineItems.Add(new OrderLineItem { Price = 10.00M, Quantity = 0, ItemTotal = 0.00M });
        lineItems.Add(new OrderLineItem { Price = 100.00M, Quantity = 0, ItemTotal = 0.00M });
        lineItems.Add(new OrderLineItem { Price = 5.00M, Quantity = 0, ItemTotal = 0.00M });

        GridView1.DataSource = lineItems;
        GridView1.DataBind();
    }
}

public class OrderLineItem
{
    public decimal Price { get; set; }
    public int Quantity { get; set; }
    public decimal ItemTotal { get; set; }
}

<强> .ASPX:

<head runat="server">
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {

            $(".QuantityClass").on('change keyup paste', function () {

                //Ctrl+Shift+J in Google Chrome to bring up the console which allows you to debug javascript
                debugger;

                var textBox = this;
                var quantity = $(textBox).val();
                var tableRows = $(textBox).parent().parent().children();

                if (quantity != "") {
                    var price = tableRows[0].children[0].innerHTML;

                    var itemTotal = price * quantity;

                    tableRows[2].children[0].innerHTML = itemTotal;
                }
                else
                    tableRows[2].children[0].innerHTML = "";
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateField HeaderText="Price">
                    <ItemTemplate>
                        <asp:Label ID="lblPrice" runat="server" Text='<%# Bind("Price")%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Quantity">
                    <ItemTemplate>
                        <asp:TextBox ID="txtQuantity" CssClass="QuantityClass" runat="server" Text='<%# Bind("Quantity")%>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Item Total">
                    <ItemTemplate>
                        <asp:Label ID="lblItemTotal" runat="server"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </form>
</body>

<强>输出:

Calculating two gridview columns client side