使用Asp.Net C#在GridView中进行计算 [英] Calculation in GridView using Asp.Net C#
本文介绍了使用Asp.Net C#在GridView中进行计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在页脚模板中进行gridview计算并编辑项目模板
我有3列GrossAmt,TDS,NetAmt
想要根据总金额和TDS计算NetAmt
NetAmt = GrossAmt-GrossAmt * TDS%
我的代码如下:
Hi,
I Need to do Calculation in gridview in footer template and edit item template
I have 3 columns GrossAmt, TDS,NetAmt
Want to calculate NetAmt based on Gross Amt and TDS
NetAmt=GrossAmt-GrossAmt*TDS%
My code as follow:
<asp:TemplateField HeaderText="Gross Amount">
<EditItemTemplate>
<asp:TextBox ID="txtGrossAmt" runat="server"/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblGrossAmt" runat="server" Text='<%#Eval("GrossAmt") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtftrGrossAmt" runat="server"/>
<asp:RequiredFieldValidator ID="rfvGrossAmt" runat="server" ControlToValidate="txtftrGrossAmt" Text="*" ValidationGroup="Validation" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TDS">
<EditItemTemplate>
<asp:DropDownList ID="ddlTDS" runat="server">
<asp:ListItem Value="0"></asp:ListItem>
<asp:ListItem Value="0.01"></asp:ListItem>
<asp:ListItem Value="0.05"></asp:ListItem>
<asp:ListItem Value="0.1"></asp:ListItem>
<asp:ListItem Value="1"></asp:ListItem>
<asp:ListItem Value="1.5"></asp:ListItem>
<asp:ListItem Value="2"></asp:ListItem>
<asp:ListItem Value="3.6"></asp:ListItem>
<asp:ListItem Value="10"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblTDS" runat="server" Text='<%#Eval("TDS") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlftrTDS" runat="server" Width="100px">
<asp:ListItem Value="0"></asp:ListItem>
<asp:ListItem Value="0.01"></asp:ListItem>
<asp:ListItem Value="0.05"></asp:ListItem>
<asp:ListItem Value="0.1"></asp:ListItem>
<asp:ListItem Value="1"></asp:ListItem>
<asp:ListItem Value="1.5"></asp:ListItem>
<asp:ListItem Value="2"></asp:ListItem>
<asp:ListItem Value="3.6"></asp:ListItem>
<asp:ListItem Value="10"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvTDS" runat="server" ControlToValidate="ddlftrTDS" Text="*" ValidationGroup="Validation"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Net Amount">
<EditItemTemplate>
<asp:TextBox ID="txtNetAmt" runat="server"/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblNetAmt" runat="server" Text='<%#Eval("NetAmt") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtftrNetAmt" runat="server"/>
<asp:RequiredFieldValidator ID="rfvNetAmt" runat="server" ControlToValidate="txtftrNetAmt" Text="*" ValidationGroup="Validation" />
</FooterTemplate>
</asp:TemplateField>
protected void gvLLDetails_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("AddNew"))
{
TextBox txtATMID = (TextBox)gvLLDetails.FooterRow.FindControl("txtftrATMID");
TextBox txtsubcode = (TextBox)gvLLDetails.FooterRow.FindControl("txtftrSUBCODE");
TextBox txtLLName = (TextBox)gvLLDetails.FooterRow.FindControl("txtftrLLName");
DropDownList ddlAggType = (DropDownList)gvLLDetails.FooterRow.FindControl("ddlftrAggType");
TextBox txtPrFrom = (TextBox)gvLLDetails.FooterRow.FindControl("txtftrPrFrom");
TextBox txtPrTo = (TextBox)gvLLDetails.FooterRow.FindControl("txtftrPrTo");
TextBox txtGrossAmt = (TextBox)gvLLDetails.FooterRow.FindControl("txtftrGrossAmt");
DropDownList ddlTDS = (DropDownList)gvLLDetails.FooterRow.FindControl("ddlftrTDS");
TextBox txtNetAmt = (TextBox)gvLLDetails.FooterRow.FindControl("txtftrNetAmt");
string ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
SqlConnection conn = new SqlConnection(ConnectionString);
conn.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO LandlordDetails(ATMID,SUBCODE,LLName,AggType,PrFrom,PrTo,GrossAmt,TDS,NetAmt)"+
"VALUES ('"+txtATMID.Text+"','"+txtsubcode.Text+"','"+txtLLName.Text+"','"+ddlAggType.SelectedValue+"','"+txtPrFrom.Text+"','"+txtPrTo.Text+
"','"+txtGrossAmt.Text+"','"+ddlTDS.Text+"','"+txtNetAmt.Text+"')",conn);
int result = cmd.ExecuteNonQuery();
conn.Close();
if (result == 1)
{
BindLandlordDetails();
lblResult.ForeColor = Color.Green;
lblResult.Text = "LandLord Details Inserted Successfully";
}
else
{
lblResult.ForeColor = Color.Red;
lblResult.Text = "Details not Inserted Successfully";
}
}
推荐答案
在插入查询之前,将您的公式应用于txtNetAmt,如: -
Before insert query apply your formula on txtNetAmt like:-
txtNetAmt.Text=txtGrossAmt.Text - txtGrossAmt.Text * ddlTDS.SelectedValue
等...
etc...
将公式放入sql查询并绑定gridview。
put formula in sql query and bind the gridview.
这篇关于使用Asp.Net C#在GridView中进行计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文