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:
Notice that you need to convert the returns from Eval() to the appropriate type. Also, notice that this code will break if any of the values are null or not formatted correctly.
You could also do it in the template by referencing a method in your code behind:
sai Kumar
Member
12 Points
14 Posts
Perform Calculation in Gridview itemtemplate
Nov 01, 2011 03:22 PM|LINK
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=nato123;"); //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();C
RichardY
Star
8376 Points
1573 Posts
Re: Perform Calculation in Gridview itemtemplate
Nov 01, 2011 03:56 PM|LINK
You can do the calculation inside the gridviews item template using databinding syntax like this:
Text='<% ((Convert.ToDecimal(Eval("ServiceAmount"))*Convert.Int32(Eval("Quantity")))*Convert.ToDouble(Eval("Discount"))).ToString() %>'Notice that you need to convert the returns from Eval() to the appropriate type. Also, notice that this code will break if any of the values are null or not formatted correctly.
You could also do it in the template by referencing a method in your code behind:
Text='<% GetCalculation(Eval("ServiceAmount"), Eval("Quantity"), Eval("Discount")) %>'Where you pass the values as strings using databinding to the GetCalculation method which you define in your code behind.
The cleanest place for the code might be in the RowDataBound event handler.
Note: Check all syntax, etc. I did not test any of these snippets.
mehta.rahuli...
Contributor
2210 Points
729 Posts
Re: Perform Calculation in Gridview itemtemplate
Nov 01, 2011 04:08 PM|LINK
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView row = e.Row.DataItem as DataRowView;
Label lblCustName = e.Row.FindControl("lblCustName") as Label;
if (lblCustName != null)
lblCustName.Text = row["custname"].ToString();
HyperLink hlEdit = e.Row.FindControl("hlEdit") as HyperLink;
if (hlEdit != null)
hlEdit.NavigateUrl = "editcust.aspx?id=" + row["id"].ToString();
}
}
C
MCAD
Contributor Award 2011
mehta.rahuli...
Contributor
2210 Points
729 Posts
Re: Perform Calculation in Gridview itemtemplate
Nov 01, 2011 04:12 PM|LINK
http://www.aspcode.net/codebehind-databinding-howto-part-1
MCAD
Contributor Award 2011
sai Kumar
Member
12 Points
14 Posts
Re: Perform Calculation in Gridview itemtemplate
Nov 01, 2011 04:22 PM|LINK
Thanks for the reply sir,
I want to know that my code is right or not ,if it is wrong please correct my code sir,i am new to programming help me to solve this problem..
tjaank
Contributor
6688 Points
1204 Posts
Re: Perform Calculation in Gridview itemtemplate
Nov 02, 2011 12:18 PM|LINK
try this:
http://weblogs.asp.net/hajan/archive/2010/08/27/calculating-gridview-total-using-javascript-jquery.aspx
Please Mark as Answer if this post helps you!