3 Replies - 5454 Views - Last Post: 01 November 2011 - 01:45 PM Rate Topic: -----

#1 rizwanit  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 11-October 11

Performing Calculation In Gridview itemtemplate

Posted 01 November 2011 - 08:56 AM



I want to perform calculation in gridview :NetAmount=(ServiceAmount*Quantity)*Discount

I had written code in rowdatabound but it is not working,i amnew to programming help me solve this proble i would appreciate if some one share ther knowledge my Axpx code is:

 <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>
                                <asp:TemplateField HeaderText="S.No">
                                    <ItemTemplate>
                                        <asp:Label ID="label10" runat="server" Font-Bold="true"  ForeColor="#BF6000" Text="<%# Container.DataItemIndex + 1 %>">
                                        </asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Serv Code" >                                     
                                    <ItemTemplate>
                                        <asp:TextBox ID="TxtServiceCode" Font-Bold="true"  ForeColor="#BF6000" Text='<%# DataBinder.Eval(Container,"DataItem.ServiceCode") %>'
                                            runat="server" Width="55px"> 
                                        </asp:TextBox>
                                  
                                        <asp:Button ID="Insert" runat="server" BackColor="Ivory" CommandName="InsertRecord"
                                            Font-Bold="True"  ForeColor="#BF6000" Text="Insert" onclick="Insert_Click"/>
                                 
                                 </ItemTemplate>
                                 </asp:TemplateField>
                                <asp:TemplateField HeaderText="Name">
                                    <ItemTemplate>
                                        <asp:TextBox ID="TxtName" runat="server" ReadOnly="true" Font-Bold="true" ForeColor="#BF6000" Width="150px" Text='<%# DataBinder.Eval(Container, "DataItem.ServiceName") %>'></asp:TextBox>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Serv Amt">
                                    <ItemTemplate>
                                        <asp:TextBox ID="TxtServiceAmount" ReadOnly="true" Font-Bold="true" ForeColor="#BF6000" Text='<%# DataBinder.Eval(Container,"DataItem.ServiceAmount") %>'
                                            runat="server" Width="55px"></asp:TextBox>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Qty">
                                    <ItemTemplate>
                                        <asp:TextBox ID="TxtQuantity" Font-Bold="true" ForeColor="#BF6000" Text='<%# DataBinder.Eval(Container,"DataItem.Quantity") %>'
                                            runat="server" Width="55px"></asp:TextBox>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Disc Amt">
                                    <ItemTemplate>
                                        <asp:TextBox ID="TxtdiscAmt" Font-Bold="true" ForeColor="#BF6000" Text='<%# DataBinder.Eval(Container,"DataItem.Discount") %>'
                                            runat="server" Width="55px"></asp:TextBox>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <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>


_____________________________________________________________

My cs code is;

              //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();



Is This A Good Question/Topic? 0
  • +

Replies To: Performing Calculation In Gridview itemtemplate

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: Performing Calculation In Gridview itemtemplate

Posted 01 November 2011 - 10:03 AM

Since all three of those values are coming from the table you could be able to do something like

<asp:TemplateField HeaderText="Net Amt">
    <ItemTemplate>
        <asp:TextBox 
            ID="TxtNetAmt" 
            ReadOnly="true" 
            Font-Bold="true" 
            ForeColor="#BF6000" 
            runat="server"
            Text='<%# Convert.ToDecimal(Eval("ServiceAmount").ToString()) * Convert.ToDecimal(Eval("Discount").ToString()) * Convert.ToDecimal(Eval("Quantity").ToString()) %>'
            Width="55px">
        </asp:TextBox>
    </ItemTemplate>
</asp:TemplateField>



and if you wanted to format it into, let's say currency, then you could do so like:

<asp:TemplateField HeaderText="Net Amt">
    <ItemTemplate>
        <asp:TextBox 
            ID="TxtNetAmt" 
            ReadOnly="true" 
            Font-Bold="true" 
            ForeColor="#BF6000" 
            runat="server"
            Text='<%# (Convert.ToDecimal(Eval("ServiceAmount").ToString()) * Convert.ToDecimal(Eval("Discount").ToString()) * Convert.ToDecimal(Eval("Quantity").ToString())).ToString("c") %>'
            Width="55px">
        </asp:TextBox>
    </ItemTemplate>
</asp:TemplateField>


This post has been edited by Nakor: 01 November 2011 - 10:08 AM

Was This Post Helpful? 0
  • +
  • -

#3 rizwanit  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 11-October 11

Re: Performing Calculation In Gridview itemtemplate

Posted 01 November 2011 - 10:14 AM

View PostNakor, on 01 November 2011 - 10:03 AM, said:

Since all three of those values are coming from the table you could be able to do something like

<asp:TemplateField HeaderText="Net Amt">
    <ItemTemplate>
        <asp:TextBox 
            ID="TxtNetAmt" 
            ReadOnly="true" 
            Font-Bold="true" 
            ForeColor="#BF6000" 
            runat="server"
            Text='<%# Convert.ToDecimal(Eval("ServiceAmount").ToString()) * Convert.ToDecimal(Eval("Discount").ToString()) * Convert.ToDecimal(Eval("Quantity").ToString()) %>'
            Width="55px">
        </asp:TextBox>
    </ItemTemplate>
</asp:TemplateField>



and if you wanted to format it into, let's say currency, then you could do so like:

<asp:TemplateField HeaderText="Net Amt">
    <ItemTemplate>
        <asp:TextBox 
            ID="TxtNetAmt" 
            ReadOnly="true" 
            Font-Bold="true" 
            ForeColor="#BF6000" 
            runat="server"
            Text='<%# (Convert.ToDecimal(Eval("ServiceAmount").ToString()) * Convert.ToDecimal(Eval("Discount").ToString()) * Convert.ToDecimal(Eval("Quantity").ToString())).ToString("c") %>'
            Width="55px">
        </asp:TextBox>
    </ItemTemplate>
</asp:TemplateField>



View PostNakor, on 01 November 2011 - 10:03 AM, said:

Since all three of those values are coming from the table you could be able to do something like

<asp:TemplateField HeaderText="Net Amt">
    <ItemTemplate>
        <asp:TextBox 
            ID="TxtNetAmt" 
            ReadOnly="true" 
            Font-Bold="true" 
            ForeColor="#BF6000" 
            runat="server"
            Text='<%# Convert.ToDecimal(Eval("ServiceAmount").ToString()) * Convert.ToDecimal(Eval("Discount").ToString()) * Convert.ToDecimal(Eval("Quantity").ToString()) %>'
            Width="55px">
        </asp:TextBox>
    </ItemTemplate>
</asp:TemplateField>



and if you wanted to format it into, let's say currency, then you could do so like:

<asp:TemplateField HeaderText="Net Amt">
    <ItemTemplate>
        <asp:TextBox 
            ID="TxtNetAmt" 
            ReadOnly="true" 
            Font-Bold="true" 
            ForeColor="#BF6000" 
            runat="server"
            Text='<%# (Convert.ToDecimal(Eval("ServiceAmount").ToString()) * Convert.ToDecimal(Eval("Discount").ToString()) * Convert.ToDecimal(Eval("Quantity").ToString())).ToString("c") %>'
            Width="55px">
        </asp:TextBox>
    </ItemTemplate>
</asp:TemplateField>




Thanks for the reply sir,
My datarow command is not working is this code correct if not suggest me steps sir what to do i am new to programming please help me to solve this problem.....
Was This Post Helpful? 0
  • +
  • -

#4 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: Performing Calculation In Gridview itemtemplate

Posted 01 November 2011 - 01:45 PM

I've done this before, and I tested it on my pc before I posted the reply just to be sure I had the syntax correct. However, I always just use the Eval("columnName") syntax but I am not sure how you are databinding so you may be required to use the DataBinder.Eval method.

You may need to replace where I have

Eval("ServiceAmount")



with

DataBinder.Eval(Container,"DataItem.ServiceAmount")


for each of the three items in that itemtemplate

This post has been edited by Nakor: 01 November 2011 - 01:47 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1