calculate row total in gridview??

Posted by Shailesh21235 under ASP.NET on 8/15/2012 | Points: 10 | Views : 2456 | Status : [Member] | Replies : 3
i have a grid view where the quantity is in text-box and when i post some value in text-box it should calculate quantity*price and show in total column????




Responses

Posted by: Vikash on: 8/15/2012 [Member] Starter | Points: 25

Up
0
Down
Plz write your complete code. so that by editing those code,
i will be able to solve the problem.

Regards,
Vikash Pathak

Shailesh21235, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 8/16/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
Hi...
Try this code.
 

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField HeaderText="S No.">
<ItemTemplate>
<asp:Label ID="lblSn" runat="server" Text='<%#Eval("Sno") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:Label ID="lblRs" runat="server" Text='<%#Eval("Price") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Qty">
<ItemTemplate>
<asp:TextBox ID="txtQty" runat="server" Text="0" OnTextChanged="txtQty_TextChanged" AutoPostBack="True">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total Amt.">
<ItemTemplate>
<asp:Label ID="lblTotalRs" runat="server" Text="0"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>



protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("Sno");
dt.Columns.Add("Price");
DataRow dr;
dr = dt.NewRow();
dr["Sno"] = "1";
dr["Price"] = "104";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Sno"] = "2";
dr["Price"] = "600";
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
// TextBox Change Event
protected void txtQty_TextChanged(object sender, EventArgs e)
{
TextBox txt = (TextBox)sender;
GridViewRow gvRow = (GridViewRow)txt.Parent.Parent;
Label lblRs = (Label)gvRow.FindControl("lblRs");
Label lblTotalRs = (Label)gvRow.FindControl("lblTotalRs");
try
{
lblTotalRs.Text = ((Convert.ToInt32(txt.Text)) * (Convert.ToInt32(lblRs.Text))).ToString();
}
catch { }
}


Shailesh21235, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shailesh21235 on: 8/16/2012 [Member] Starter | Points: 25

Up
0
Down
hey vikas mine is also the similar code.....
hey ranjeet do i need to create a datatable also...than why is it in postback....only the textchanged function wont work???
Regards..

Shailesh21235, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response