在Gridview Itemtemplate中计算?

时间:2016-04-14 02:36:25

标签: c# asp.net gridview

我在Gridview中有两个itemTemplate,需要执行乘法运算并将其存储在另一列中,我该怎么做?即乘以productPriceproductQuantity并存储在totalPrice标签中。谢谢

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:AutopartConnectionString %>" 
     SelectCommand="SELECT [id], [productID], [productName], [productPrice], [productQuantity], [totalPrice] FROM [cartDetails]"
     UpdateCommand="UPDATE [cartDetails] SET [productQuantity]=@productQuantity WHERE [productID]=@productID"
     DeleteCommand="DELETE [cartDetails] WHERE [productID]=@productID">
        <%--<UpdateParameters>
            <asp:Parameter Name="productQuantity" Type="Int32" />
            <asp:Parameter Name="productQuantity" Type="String" />
       </UpdateParameters>--%>
        <DeleteParameters>
             <asp:Parameter Name="productID" Type="Int32" />
        </DeleteParameters>
    </asp:SqlDataSource>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource1">
        <Columns>
            <asp:TemplateField HeaderText="id" InsertVisible="False" SortExpression="id" Visible="False">
                <EditItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="productID" SortExpression="productID" >

                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Eval("productID") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="productName" SortExpression="productName">

                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Eval("productName") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="productPrice" SortExpression="productPrice">
                <ItemTemplate>
                    <asp:Label ID="Label4" runat="server" Text='<%# Eval("productPrice") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="productQuantity" SortExpression="productQuantity">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox4" runat="server" Text='<%# Eval("productQuantity") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label5" runat="server" Text='<%# Eval("productQuantity") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:CommandField ShowEditButton="True" />
            <asp:TemplateField HeaderText="totalPrice" SortExpression="totalPrice">

                <ItemTemplate>
                    <asp:Label ID="Label6" runat="server" Text='<%# ((Convert.ToDecimal(Eval("productPrice")))*(Convert.ToDecimal(Eval("productQuantity")))).ToString() %>'></asp:Label>
                   <%-- <asp:Label ID="Label6" runat="server" Text='<%# Bind("totalPrice") %>'></asp:Label>--%>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:CommandField ShowDeleteButton="True" />
        </Columns>
    </asp:GridView>

我的表结构

Create table cartDetails(
id int not null identity(1,1) primary key,
productID int not null,
productName VARCHAR(150) not null ,
productPrice float,
productQuantity int,
totalPrice float
);

1 个答案:

答案 0 :(得分:0)

有没有理由不在Select进行数学计算?或者你甚至可以在表设计器中将totalPrice作为计算字段。

SELECT [id]
    ,  [productID]
    ,  [productName]
    ,  [productPrice]
    ,  [productQuantity]
    ,  [productPrice] * [productQuantity] as [totalPrice] 
    FROM [cartDetails]

然后绑定totalPrice

<ItemTemplate>
  <asp:Label ID="Label6" runat="server" Text='<%# Bind("totalPrice") %>'</asp:Label>
</ItemTemplate>

另外,关于ItemTemplate内的数学;你必须使用Eval(),你必须确保没有空值。根据需要在isnull(<field>,0)声明中明智地使用SELECT

相关问题