locked
Calculate Column Value RRS feed

  • Question

  • User-1499457942 posted

    Hi

     I want when there is change in Qty column . Amount value should get it calculated accordingly. If Rate is 100 And Qty is 10 then Amount should be calculated as 1000.

    Secondly value in Qty should be between 1 and 200 otherwise it should give error.

    <asp:GridView ID="gvw"  runat="server" 
                    
                    EmptyDataText="No Records Found!" EmptyDataRowStyle-ForeColor="Red" EmptyDataRowStyle-CssClass ="gvEmpty" >
                    <HeaderStyle ForeColor="White" Font-Bold="True" BackColor="#428bca"></HeaderStyle>
                    <Columns>
                        <asp:templatefield headertext="Emp.Code">
                            <itemtemplate>
                                <asp:label runat="server" id="lblcode" text='<%#Eval("EmployeeId") %>'></asp:label>
                            </itemtemplate>
                        </asp:templatefield>
                        
    <asp:TemplateField HeaderText = "Rate">
                            <ItemTemplate>
                                <asp:TextBox ID="txtRate" Text='<%# Eval("Rate") %>'  CssClass="form-control" runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
    <asp:TemplateField HeaderText = "Rate">
                            <ItemTemplate>
                                <asp:TextBox ID="txtQty" Text='<%# Eval("QTy") %>'  CssClass="form-control" runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
    
                        <asp:templatefield headertext="Amount">
                            <itemtemplate>
                                <asp:label runat="server" id="lblAmount" text='<%#Eval("BaseAmount") %>'></asp:label>
                            </itemtemplate>
                        </asp:templatefield>
    
                    </Columns>
                    </asp:GridView>

    Thanks

    Monday, November 19, 2018 6:01 AM

All replies

  • User-271186128 posted

    Hi JagjitSingh,

    JagjitSingh

     I want when there is change in Qty column . Amount value should get it calculated accordingly. If Rate is 100 And Qty is 10 then Amount should be calculated as 1000.

    Secondly value in Qty should be between 1 and 200 otherwise it should give error.

    You could refer to the following code to use JQuery validate and calculate the value.

    JQuery Script:

        <script src="../Scripts/jquery-1.10.2.min.js"></script>
        <script type="text/javascript">
            $(function () {
                $("#gvw tr").find("input[id*='txtRate']").each(function (index, item) {
                    $(item).change(function () {
                        //get rate and qty
                        var rate = $(this).val();
                        var qty = $(this).closest("tr").find("input[id*='txtQty']").val();
    //check whether the value is numeric. if ($.isNumeric(rate) && $.isNumeric(qty)) { $(this).closest("tr").find("[id*='lblAmount']").html(rate * qty); } }); }); $("#gvw tr").find("input[id*='txtQty']").each(function (index, item) { $(item).change(function () { //get qty var qty = $(this).val(); //validate the value. if ($.isNumeric(qty) && parseInt(qty) >=1 && parseInt(qty)<=200) {
    //remove the border color. $(this).css('border-color', ''); var rate = $(this).closest("tr").find("input[id*='txtRate']").val(); if ($.isNumeric(rate)) { $(this).closest("tr").find("[id*='lblAmount']").html(rate * qty); } } else{
    //validate fail, change the border color. $(this).css('border-color', 'red');
    //focus this textbox to modify the value. $(this).focus();
    //display the error message. alert("Please check the qty value, minimum 1, maximum 200") } }); }); }); </script>
                <asp:GridView ID="gvw"  runat="server" 
                     AutoGenerateColumns="false"
                    EmptyDataText="No Records Found!" EmptyDataRowStyle-ForeColor="Red" EmptyDataRowStyle-CssClass ="gvEmpty" >
                    <HeaderStyle ForeColor="White" Font-Bold="True" BackColor="#428bca"></HeaderStyle>
                    <Columns>
                        <asp:templatefield headertext="Emp.Code">
                            <itemtemplate>
                                <asp:label runat="server" id="lblcode" text='<%#Eval("EmployeeId") %>'></asp:label>
                            </itemtemplate>
                        </asp:templatefield>
                        
    <asp:TemplateField HeaderText = "Rate">
                            <ItemTemplate>
                                <asp:TextBox ID="txtRate" Text='<%# Eval("Rate") %>'  CssClass="form-control" runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
    <asp:TemplateField HeaderText = "Rate">
                            <ItemTemplate>
                                <asp:TextBox ID="txtQty" Text='<%# Eval("QTy") %>'  CssClass="form-control" runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
    
                        <asp:templatefield headertext="Amount">
                            <itemtemplate>
                                <asp:label runat="server" id="lblAmount" text='<%#Eval("BaseAmount") %>'></asp:label>
                            </itemtemplate>
                        </asp:templatefield>
    
                    </Columns>
                    </asp:GridView>

    the output as below:

    Best regards,
    Dillion

    Tuesday, November 20, 2018 3:10 AM