Click here to Skip to main content
15,891,762 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to show the total of two rows in gridview , please help me to perform this , really I don't know how to code this, what I have the code is,

C#
protected void btnAdd_Click(object sender, EventArgs e)
{
    objDetLst.Clear();
    objDet = new SalesDetails();
    objDet.TrID = 0;
    objDet.ItemCode = hidItemCode.Text;
    objDet.Quantity = Convert.ToDecimal(txtQuantity.Text);
    objDet.Price = 0;
    objDetLst.Add(objDet);
    CreateList();
    LoadGridView();
    txtItemCode.Text = "";
    txtQuantity.Text = "";
    decimal  total = 0;
    total = Convert.ToDecimal(objDet.Quantity) * Convert.ToDecimal(objDet.Price);
    total = grvList.Rows[0].Cells[4].Text ;




}


when I press add button should add the item code, qty , price and total.
how I can do it. please help me.
Posted
Comments
Sinisa Hajnal 6-Oct-14 6:16am    
Your price is zero, your total will always be zero.

You're also clearing the list on each call to Add so you would always have only one item in the list? What do CreateList and LoadGridView do? That is, what is datasource for GridView and what does CreateList do?
Not clear with this code. Still, what are you doing by the below code?

total = grvList.Rows[0].Cells[4].Text;
Leena 206 6-Oct-14 6:44am    
do you want to do something like this? http://www.aspsnippets.com/Articles/Adding-Dynamic-Rows-in-ASP.Net-GridView-Control-with-TextBoxes.aspx
Leena 206 6-Oct-14 6:46am    
you may try like this also: http://dbakings.com/ASP/Aspinsertnewrowingridview.aspx
tastini 6-Oct-14 7:46am    
sorry that my doubt is not clear. I am trying to add items into gridview, after that I save the gridview to database. my issue is when I am adding the items to gridview, I have 2 data, 1.qty 2. price, I want to show the total in third row. I mean multiple the data and qty show this data to total column. All the other data are coming except the total.

Try this,

find the two labels(i.e, quantity and price) from your gridview on row data bound and multiply two values and bind it in total column,
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    
        label lqty=(label)e.Row.findcontrol("Quantity_labelid_from Grid");
        label lprice=(label)e.Row.findcontrol("Price_id_from Grid");
        
        float total=float.parse(lqty.text)*float.parse(lprice.text);
        
        label ltot=(label)e.Row.findcontrol("Total_columnId");
        ltot.text=total.tostring();   
    }
}



[Edit member="Tadit"]
Added pre tags.
[/Edit]
 
Share this answer
 
v2
Comments
Good answer. My 5. :)
Rajesh waran 18-Oct-14 6:09am    
Thank You @Tadit Dash.
Welcome. :)
tastini 6-Oct-14 8:18am    
<div style="clear"></div>
<asp:GridView ID="grvList" runat="server" AutoGenerateColumns="False" CssClass="gridview" GridLines="Horizontal" OnRowDataBound="grvList_RowDataBound">
<alternatingrowstyle cssclass="alternate">
<columns> <asp:BoundField HeaderText="Sl. No.">
<HeaderStyle Width="60px" />
<asp:TemplateField HeaderText="Item">
<itemtemplate>
<asp:Label ID="txtItemCode" Text='<%# Bind("ItemCode") %>' runat="server">


<asp:TemplateField HeaderText="Quantity">
<itemtemplate>
<asp:TextBox ID="txtQuantity" Width="80px" TextMode="SingleLine" Text='<%# Bind("Quantity") %>' runat="server">

<HeaderStyle Width="100px" />

<asp:TemplateField HeaderText="Price">
<itemtemplate>
<asp:TextBox ID="txtPrice" Width="80px" TextMode="SingleLine" Text='<%# Bind("Price") %>' runat="server">

<HeaderStyle Width="100px" />

<asp:TemplateField HeaderText="Total">
<itemtemplate>
<asp:TextBox ID="txttotal" Width="80px" TextMode="SingleLine" runat="server">

<HeaderStyle Width="80px" />



this is my asp code.... you can help me in this code please.
protected void grvList_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    
                TextBox tqty = (TextBox)e.Row.FindControl("txtQuantity");
                TextBox tprice = (TextBox)e.Row.FindControl("txtPrice");

                float total = float.Parse(tqty.Text) * float.Parse(tprice.Text);

                TextBox ttot = (TextBox)e.Row.FindControl("txttotal");
                ttot.Text = total.ToString(); 
    }
}
 
Share this answer
 
Comments
Rajesh waran 6-Oct-14 9:22am    
I think it should be helpful to you?
tastini 6-Oct-14 9:24am    
let me try, I am getting some other errors now, I will update you once I got run the program.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900