I have a problem. In my page, there have 3 gridview.
Every gridview has sub total displaying at its footer.
I am able to display the sub total for each gridview at its footer.
But, i want to display one more total that is grand total.
The value for the grand total is the total of sub total from each gridview.
I want to display the grand total value using label.
I don't know how to do it and I don't have any idea how to take the subtotal value from each gridview to make the calculation.

## All 11 Replies

You can do this thing like in below way :

I assume you are showing sub total of all three gridview in TextBox in footerrow. So your code may look like.

TextBox t1 = yourfirstGridView.FooterRow().FindControl(“control id where you are showing subtotal”)

TextBox t2 = yoursecondGridView.FooterRow().FindControl(“control id where you are showing subtotal”)

TextBox t3 = yourthirdGridView.FooterRow().FindControl(“control id where you are showing subtotal”)

now for GrandTotal, you need to convert in using
int intGrandTotal = Convert.ToInt(t1.Text) + Convert.ToInt(t2.Text) + Convert.ToInt(t3.Text)
your grand total label/textbox = intGrandTotal.ToString();

or you can do this by below way also :

when you calculate sub total for each gridview and displaying it in label or textbox, at that time also store in ViewState like
ViewState(“subtotal1”) = subtotal 1
ViewState(“subtotal2”) = subtotal 2
ViewState(“subtotal3”) = subtotal 3

then same

int intGrandTotal = Convert.ToInt(ViewState(“subtotal1”) .ToStrin()) + Convert.ToInt(ViewState(“subtotal2”) .ToString()) + Convert.ToInt(ViewState(“subtotal3”) .ToString())
your grand total label/textbox = intGrandTotal.ToString();

try it and let us know :-)

I have a problem. In my page, there have 3 gridview.
Every gridview has sub total displaying at its footer.
I am able to display the sub total for each gridview at its footer.
But, i want to display one more total that is grand total.
The value for the grand total is the total of sub total from each gridview.
I want to display the grand total value using label.
I don't know how to do it and I don't have any idea how to take the subtotal value from each gridview to make the calculation.

Sorry i didn't post my coding for the subtotal before.
Here is my coding to display subtotal:

``````public partial class ReportAllPipeline : System.Web.UI.Page
{
decimal total1 = 0;
decimal total2 = 0;
decimal total3 = 0;
decimal total4 = 0;
decimal total5 = 0;
decimal total6 = 0;

protected void Page_Load(object sender, EventArgs e)
{

}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
total1 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "project_revenue"));
total2 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "cost"));
}

else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Text = "Total:";
e.Row.Cells[2].Text = total1.ToString("c");
e.Row.Cells[3].Text = total2.ToString("c");
e.Row.Font.Bold = true;
}
}

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
total3 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "project_revenue"));
total4 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "cost"));
}

else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Text = "Total:";
e.Row.Cells[2].Text = total3.ToString("c");
e.Row.Cells[3].Text = total4.ToString("c");
e.Row.Font.Bold = true;
}
}

protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
total5 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "project_revenue"));
total6 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "cost"));
}

else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Text = "Total:";
e.Row.Cells[2].Text = total5.ToString("c");
e.Row.Cells[3].Text = total6.ToString("c");
e.Row.Font.Bold = true;
}
}
}``````

I have try to display the grand total.But, i got errors.
I am try to display the grand total using label.
Here is my code:

``````int totalRevenue = Convert.ToInt32(GridView1.FooterRow.Cells[2].Text) + Convert.ToInt32(GridView2.FooterRow.Cells[2].Text) + Convert.ToInt32(GridView3.FooterRow.Cells[2].Text);
int totalProfit = Convert.ToInt32(GridView1.FooterRow.Cells[3].Text) + Convert.ToInt32(GridView2.FooterRow.Cells[3].Text) + Convert.ToInt32(GridView3.FooterRow.Cells[3].Text);

grandRevenue.Text = totalRevenue.ToString();
grandProfit.Text = totalProfit.ToString();``````

The error are:
Invalid token '=' in class, struct, or interface member declaration
Invalid token '(' in class, struct, or interface member declaration
Invalid token '=' in class, struct, or interface member declaration
Invalid token '(' in class, struct, or interface member declaration

i don't know why the error occur.Maybe there is something wrong with my coding.
Hope that someone can go through my coding and tell me what's wrong and give me the suggestion on how to display the grand total.

try to execute below modified code.

int totalRevenue = Convert.ToInt32(GridView1.FooterRow().Cells[2].Text) + Convert.ToInt32(GridView2.FooterRow().Cells[2].Text) + Convert.ToInt32(GridView3.FooterRow().Cells[2].Text);

int totalProfit = Convert.ToInt32(GridView1.FooterRow().Cells[3].Text) + Convert.ToInt32(GridView2.FooterRow().Cells[3].Text) + Convert.ToInt32(GridView3.FooterRow().Cells[3].Text);

grandRevenue.Text = totalRevenue.ToString();
grandProfit.Text = totalProfit.ToString();

Dear Rohand,
Your code seems same like the code that i have write before.
I have try to execute your code and i still get the same errors.

try to execute below modified code.

int totalRevenue = Convert.ToInt32(GridView1.FooterRow().Cells[2].Text) + Convert.ToInt32(GridView2.FooterRow().Cells[2].Text) + Convert.ToInt32(GridView3.FooterRow().Cells[2].Text);

int totalProfit = Convert.ToInt32(GridView1.FooterRow().Cells[3].Text) + Convert.ToInt32(GridView2.FooterRow().Cells[3].Text) + Convert.ToInt32(GridView3.FooterRow().Cells[3].Text);

grandRevenue.Text = totalRevenue.ToString();
grandProfit.Text = totalProfit.ToString();

Have you tired below part from my first thread? If not then try it by storing the value in viewstate and use it.

when you calculate sub total for each gridview and displaying it in label or textbox, at that time also store in ViewState like
ViewState(“subtotal1”) = subtotal 1
ViewState(“subtotal2”) = subtotal 2
ViewState(“subtotal3”) = subtotal 3

then same

int intGrandTotal = Convert.ToInt(ViewState(“subtotal1”) .ToStrin()) + Convert.ToInt(ViewState(“subtotal2”) .ToString()) + Convert.ToInt(ViewState(“subtotal3”) .ToString())
your grand total label/textbox = intGrandTotal.ToString();

Dear Rohand,
Your code seems same like the code that i have write before.
I have try to execute your code and i still get the same errors.

I am not sure what to put in the "subtotal". Is it the name of textbox?
or the name of subtotal parameter?

I have try this:

``````protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
total5 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "project_revenue"));
total6 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "cost"));
}

else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Text = "Total:";
e.Row.Cells[2].Text = total5.ToString("c");
ViewState("e.Row.Cells[2].Text") = total5;
e.Row.Cells[3].Text = total6.ToString("c");
ViewState("e.Row.Cells[3].Text") = total6;
e.Row.Font.Bold = true;
}``````

but, i got error.The error is:
The name 'ViewState' does not exist in the current context

you can store any meaningful name like :
ViewState["total5"] = total5;
ViewState["total6"] = total6;

in above way for all your Gridviews subtotal and at the time of Grandtotal see the code from my previous post.

Hope now it's clear and solve your problem..

I am not sure what to put in the "subtotal". Is it the name of textbox?
or the name of subtotal parameter?

I have try this:

``````protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
total5 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "project_revenue"));
total6 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "cost"));
}

else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Text = "Total:";
e.Row.Cells[2].Text = total5.ToString("c");
ViewState("e.Row.Cells[2].Text") = total5;
e.Row.Cells[3].Text = total6.ToString("c");
ViewState("e.Row.Cells[3].Text") = total6;
e.Row.Font.Bold = true;
}``````

but, i got error.The error is:
The name 'ViewState' does not exist in the current context

i have try to do the following code, but i still got error.
the code:

``````protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
total1 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "project_revenue"));
ViewState("subtotal1") = total1;
total2 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "cost"));
ViewState("subtotal2") = total2;
}

else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Text = "Total:";
e.Row.Cells[2].Text = total1.ToString("c");
e.Row.Cells[3].Text = total2.ToString("c");
e.Row.Font.Bold = true;
}
}``````

the error is:
The name 'ViewState' does not exist in the current context

in C# , you need to use this "[ ]" instead of "( ) " in viewstate.

try the code i have posted in thread.

i have try to do the following code, but i still got error.
the code:

``````protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
total1 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "project_revenue"));
ViewState("subtotal1") = total1;
total2 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "cost"));
ViewState("subtotal2") = total2;
}

else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Text = "Total:";
e.Row.Cells[2].Text = total1.ToString("c");
e.Row.Cells[3].Text = total2.ToString("c");
e.Row.Font.Bold = true;
}
}``````

the error is:
The name 'ViewState' does not exist in the current context

i have try to make changes to my code.
But i keep get error.
now, i will post my complete code.
hope that someone can go through my code and help me to settle the problem.

``````public partial class ReportAllPipeline : System.Web.UI.Page
{
decimal total1 = 0;
decimal total2 = 0;
decimal total3 = 0;
decimal total4 = 0;
decimal total5 = 0;
decimal total6 = 0;

protected void Page_Load(object sender, EventArgs e)
{

}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
total1 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "project_revenue"));
ViewState["subtotal1"] = total1;
total2 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "cost"));
ViewState["subtotal2"] = total2;
}

else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Text = "Total:";
e.Row.Cells[2].Text = total1.ToString("c");
e.Row.Cells[3].Text = total2.ToString("c");
e.Row.Font.Bold = true;
}
}

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
total3 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "project_revenue"));
ViewState["subtotal3"] = total3;
total4 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "cost"));
ViewState["subtotal4"] = total4;
}

else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Text = "Total:";
e.Row.Cells[2].Text = total3.ToString("c");
e.Row.Cells[3].Text = total4.ToString("c");
e.Row.Font.Bold = true;
}
}

protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
total5 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "project_revenue"));
ViewState["subtotal5"] = total5;
total6 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "cost"));
ViewState["subtotal6"] = total6;
}

else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Text = "Total:";
e.Row.Cells[2].Text = total5.ToString("c");
e.Row.Cells[3].Text = total6.ToString("c");
e.Row.Font.Bold = true;
}
}

decimal totalRev = Convert.ToDecimal(viewState["subtotal1"].ToString()) + Convert.ToDecimal(ViewState["subtotal3"].ToString()) + Convert.ToDecimal(ViewState["subtotal5"].ToString());
grandRevenue.Text = totalRev.ToString();

decimal totalProf = Convert.ToDecimal(ViewState["subtotal2"].ToString()) + Convert.ToDecimal(ViewState["subtotal4"].ToString()) + Convert.ToDecimal(ViewState["subtotal6"].ToString());
grandProfit.Text = totalProf.ToString();
}``````

the error that i got:
Invalid token '=' in class, struct, or interface member declaration
Invalid token '(' in class, struct, or interface member declaration
Invalid token '=' in class, struct, or interface member declaration
Invalid token '(' in class, struct, or interface member declaration

i think the error occur because i try to call value from gridview in "public partial class ReportAllPipeline" not in "protected void GridView_RowDataBound()"

i don't know what to do and how to fix it.

I believe your ViewState[""] = your subtotal should be in else if part. I mean under Footer condition.

else if (e.Row.RowType == DataControlRowType.Footer)

i have try to make changes to my code.
But i keep get error.
now, i will post my complete code.
hope that someone can go through my code and help me to settle the problem.

``````public partial class ReportAllPipeline : System.Web.UI.Page
{
decimal total1 = 0;
decimal total2 = 0;
decimal total3 = 0;
decimal total4 = 0;
decimal total5 = 0;
decimal total6 = 0;

protected void Page_Load(object sender, EventArgs e)
{

}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
total1 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "project_revenue"));
ViewState["subtotal1"] = total1;
total2 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "cost"));
ViewState["subtotal2"] = total2;
}

else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Text = "Total:";
e.Row.Cells[2].Text = total1.ToString("c");
e.Row.Cells[3].Text = total2.ToString("c");
e.Row.Font.Bold = true;
}
}

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
total3 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "project_revenue"));
ViewState["subtotal3"] = total3;
total4 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "cost"));
ViewState["subtotal4"] = total4;
}

else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Text = "Total:";
e.Row.Cells[2].Text = total3.ToString("c");
e.Row.Cells[3].Text = total4.ToString("c");
e.Row.Font.Bold = true;
}
}

protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
total5 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "project_revenue"));
ViewState["subtotal5"] = total5;
total6 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "cost"));
ViewState["subtotal6"] = total6;
}

else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[1].Text = "Total:";
e.Row.Cells[2].Text = total5.ToString("c");
e.Row.Cells[3].Text = total6.ToString("c");
e.Row.Font.Bold = true;
}
}

decimal totalRev = Convert.ToDecimal(viewState["subtotal1"].ToString()) + Convert.ToDecimal(ViewState["subtotal3"].ToString()) + Convert.ToDecimal(ViewState["subtotal5"].ToString());
grandRevenue.Text = totalRev.ToString();

decimal totalProf = Convert.ToDecimal(ViewState["subtotal2"].ToString()) + Convert.ToDecimal(ViewState["subtotal4"].ToString()) + Convert.ToDecimal(ViewState["subtotal6"].ToString());
grandProfit.Text = totalProf.ToString();
}``````

the error that i got:
Invalid token '=' in class, struct, or interface member declaration
Invalid token '(' in class, struct, or interface member declaration
Invalid token '=' in class, struct, or interface member declaration
Invalid token '(' in class, struct, or interface member declaration

i think the error occur because i try to call value from gridview in "public partial class ReportAllPipeline" not in "protected void GridView_RowDataBound()"

i don't know what to do and how to fix it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.