2016-04-14 34 views
0

Gridview'de iki itemTemplate var, çarpma işlemini gerçekleştirmeli ve başka bir sütunda saklamalıyım, Bunu nasıl yaparım? productPrice ve productQuantity ile çarpın ve totalPrice etiketinde saklayın. Teşekkür SenHesaplama?

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:AutopartConnectionString %>" 
    SelectCommand="SELECT [id], [productID], [productName], [productPrice], [productQuantity], [totalPrice] FROM [cartDetails]" 
    UpdateCommand="UPDATE [cartDetails] SET [productQuantity][email protected] WHERE [productID][email protected]" 
    DeleteCommand="DELETE [cartDetails] WHERE [productID][email protected]"> 
     <%--<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> 

Benim Tablo yapısı

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

Kodunuzu çalıştırdığınızda ne olur? TotalPrice'de bir şey görüyor musunuz? – ConnorsFan

+0

@ConnorsFan Bu 'Derleyici Hata İletisini Aldım: CS0103: 'Bağlam' adı geçerli bağlamda mevcut değil ' – adarshjaya12

+1

Tamam. Yani, hesaplama sütunu olmadan bile, bazı sorunlarınız olmalı. Tüm sütunlarda 'Eval' ile' Bind' değiştirebilirsiniz. – ConnorsFan

cevap

0

Select matematik yapmadığı için bir sebep var mı? ya da totalPrice'yi masa tasarımcısında hesaplanmış bir alan haline getirebilirsiniz.

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

ve sonra sadece bir ItemTemplate içinde matematik konusunda, totalPrice

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

Ayrıca bağlamak; Eval() kullanmalı ve boş değerlerin olmadığından emin olmalısınız. isnull(<field>,0)'un SELECT ifadesinde gerektiği gibi kullanılması.