簡體   English   中英

在Gridview中執行計算itemtemplate

[英]Performing Calculation In Gridview itemtemplate

我想在GridView執行計算:

NetAmount = (ServiceAmount * Quantity) * Discount

我已經在RowDataBound編寫了代碼,但無法正常工作。 有人可以幫我解決這個問題嗎? 如果有人分享他們的知識,我將不勝感激。

我的Aspx代碼:

<asp:GridView ID="GridView1" runat="server" Height="156px" Width="618px"
    AutoGenerateColumns="False" BorderWidth="1px" 
    HorizontalAlign="Justify" onrowdatabound="GridView1_RowDataBound" 
    BackColor="LightGoldenrodYellow" BorderColor="Tan" CellPadding="2" 
    ForeColor="Black" GridLines="None" onrowcommand="GridView1_RowCommand"> 

    <FooterStyle BackColor="Tan" />
    <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
        <HeaderStyle BackColor="#FFFFC4" Font-Bold="True" ForeColor="#BF6000"/>
        <Columns>
            <!-- snip -->
            <asp:TemplateField HeaderText="Net Amt">
                <ItemTemplate>
                    <asp:TextBox ID="TxtNetAmt" ReadOnly="true" Font-Bold="true" ForeColor="#BF6000" runat="server" Width="55px"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    <AlternatingRowStyle BackColor="PaleGoldenrod" />
</asp:GridView>


<script type="text/javascript">
    function CalcSercode(ServiceAmount,Quantity, Discount,NetAmount)
    {
        var Quantity=parseFloat(document.getElementById(Quantity).value);
        var Discount= parseFloat(document.getElementById(Discount).value);
        var ServiceAmount=document.getElementById(ServiceAmount);
        var SerAmountValue = parseFloat((ServiceAmount * Quantity)-((ServiceAmount  *Quantity)*Discount/100));
        //var SellPriceValueRound = Math.round(SellPriceValue,4);
        var SerAmountValueRound = SerAmountValue;
        ServiceAmount.innerHTML= SerAmountValueRound ;
    }
</script>

我的CS代碼:

//TextBox TxtServiceCode =  GridView1.Controls[0].Controls[0].FindControl("TxtServiceCode") as TextBox;
//DataSet dss = new DataSet();
//SqlConnection MyConnection = new SqlConnection("server=prog; database=mydatabase;UID=sa;PWD=naco123;");
//SqlCommand sqlcmd = new SqlCommand("select * from [ServiceCode]",  MyConnection);
//SqlDataAdapter adp = new SqlDataAdapter(sqlcmd);
//DataSet ds = new DataSet();
//adp.Fill(ds);
//GridView1.DataSource = ds.Tables[0];
//GridView1.DataBind();

您應該能夠使用內聯數據綁定語法進行該計算:

<ItemTemplate>
    <%# ((Convert.ToDecimal(Eval("ServiceAmount")) * Convert.ToInt32(Eval("Quantity"))) * Convert.ToDecimal(Eval("Discount"))).ToString("C") %>
</ItemTemplate>

通常,我建議您不要在前端進行此計算,而應該在業務層或像您這樣的情況下進行計算,您似乎沒有一個可以直接進入數據庫。

因此,請勿將NetAmount =(ServiceAmount * Quantity)* Discount添加到您的選擇語句中。 如果這樣做,您可以簡單地將該列數據綁定到網格中,因為它始終是只讀的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM