calculate running total in a gridview and displayed in each row..

Posted by Gopal_nivas under ASP.NET on 2/15/2011 | Points: 10 | Views : 31525 | Status : [Member] | Replies : 16
hi..

i have a gridview..

in tat i was binding the table values like name,price and two textboxes for quantity and another one for total textbox for calulating total depends on quantity with price.

i.e)
if i enetring some quantites during tat time it will caluclate the total value and displayed in total textbox..
i dont want to use button to calculate the total value for each rows.

how to do this..

wat is the eacy way to do this..

need ur suggestions with examples..

regards
gopal.s




Responses

Posted by: Prabhakar on: 2/15/2011 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hi Gopal_nivas


it's very easy going on you get total in Footer Tempalte in A Gridview Like This

in a grid view
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="SqlDataSource1"
OnRowDataBound="GridView1_RowDataBound"
ShowFooter="True" AllowPaging="True" PageSize="5"
BackColor="#ffffff" BorderColor="AliceBlue"
BorderStyle="None" BorderWidth="1px"
CellPadding="3"
CellSpacing="2" FooterStyle-BackColor="#da821e"
FooterStyle-ForeColor="#ffffff"
RowStyle-BackColor="#003366"
RowStyle-ForeColor="#ffffff"
AlternatingRowStyle-BackColor="#da821e">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID"
InsertVisible="False" ReadOnly="True"
SortExpression="ID" />
<asp:BoundField DataField="Name" HeaderText="Name"
InsertVisible="False" ReadOnly="True"
SortExpression="Name" FooterText="Total"/>
<asp:TemplateField HeaderText="Amount">
<ItemTemplate>
<asp:Label ID="lblAmount" runat="server"
Text='<%# "$"+Eval("Amount").ToString()%>'>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal" runat="server"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<HeaderStyle BackColor="#da821e" Font-Bold="True"
ForeColor="White" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ID], [Name], [Amount] FROM [Expenses]">
</asp:SqlDataSource>

in aspx page

public partial class _Default : System.Web.UI.Page
{
decimal grdTotal = 0;
protected void Page_Load(object sender, EventArgs e)
{

}
.protected void GridView1_RowDataBound
(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
decimal rowTotal = Convert.ToDecimal
(DataBinder.Eval(e.Row.DataItem, "Amount"));
grdTotal = grdTotal + rowTotal;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lbl = (Label)e.Row.FindControl("lblTotal");
lbl.Text = grdTotal.ToString("c");
}
}
}

it's a use full for you . .

Best Regard's
Prabhakar

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

Posted by: Gopal_nivas on: 2/15/2011 [Member] Starter | Points: 25

Up
0
Down
hi prabaha..

thanks for ur reply..

but my requirement will be to show the total in each row according to the quantity .

how to do this..

regards
gopal.s



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

Posted by: Karthikanbarasan on: 2/15/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
You have to loop through the rows and sum it up. Something like the following:

double Total = 0;
foreach(GridViewRow row in MyGridView.Rows)
Total += Convert.ToDouble(row.Cells[PRICE_COLUMN].Text);

Thanks
Karthik
www.f5Debug.net

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

Posted by: Gopal_nivas on: 2/15/2011 [Member] Starter | Points: 25

Up
0
Down
hi karthik..

in which gridview event..

is it rowdatabound or some this else..

regards
gopal.s



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

Posted by: Prabhakar on: 2/15/2011 [Member] [MVP] Starter | Points: 25

Up
0
Down
Hi Gopal_nivas ,,

u do this on GridView1_RowDataBound . .

Best Regard's
Prabhakar

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

Posted by: Karthikanbarasan on: 2/15/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
yes u can use in the row data bound event!!!

Thanks
Karthik
www.f5Debug.net

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

Posted by: Madhu.b.rokkam on: 2/15/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
You can use Javascript to do this at runtime, Else as suggested by Karthik when loading the page if you need you can do that on RowDataBound of the gridview.

Thanks and Regards
Madhu

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

Posted by: Gopal_nivas on: 2/15/2011 [Member] Starter | Points: 25

Up
0
Down
hi madhu..

thnx for ur reply..
i have to give value during runtime..
then how can i goto the row databound event.

if i enter some values in text box it caluclutes the total..
if u have any examples plz provide me..

regards
gopal.s

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

Posted by: Karthikanbarasan on: 2/15/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Hi Gopal,

At runtime mean? whats your exact requirement?

Thanks
Karthik
www.f5Debug.net

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

Posted by: Madhu.b.rokkam on: 2/15/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
What he means is after loading the page he wants to calculate the total when he changes values in the textboxes that were on grid.

Yes Gopal I have done similar thing before I will share that with you mostly by tomorrow.


Thanks and Regards
Madhu

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

Posted by: Gopal_nivas on: 2/16/2011 [Member] Starter | Points: 25

Up
0
Down
thanks madhu..

i am waiting for ur reply..

regards
gopal.s

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

Posted by: Madhu.b.rokkam on: 2/16/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi Gopal,

I have done a sample on this and i will post it as an article today for you.



Thanks and Regards
Madhu

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

Posted by: Madhu.b.rokkam on: 2/16/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
I have posted the article today, Once that gets approved by the webmaster you can download the sample as well as go thru the code if you feel any other doubt you can ask me.

Thanks and Regards
Madhu

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

Posted by: Gopal_nivas on: 2/17/2011 [Member] Starter | Points: 25

Up
0
Down
Thanks madhu..

ur article helped me lot..

regards
gopal.s

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

Posted by: Madhu.b.rokkam on: 2/17/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Thanks Gopal..

It makes me happy that i could help someone resolve his issue.
Request you to comment me on that article itself.


Thanks and Regards
Madhu

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

Posted by: Madhu.b.rokkam on: 2/17/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi WebMaster,

I think we can change the status of this to resolved.

Thanks and Regards
Madhu

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

Login to post response