Click here to Skip to main content
15,881,636 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
//page.aspx

ASP.NET
<asp:GridView ID="gvProd" CssClass="Gridview" HeaderStyle-BackColor="#BC5378"onrowdatabound="gvProd_RowDataBound" HeaderStyle-ForeColor="White" HeaderStyle-Font-Bold ="true"  runat="server" ShowFooter="true" AllowPaging="true" PageSize="5" AutoGenerateColumns="false" onpageindexchanging="gvProd_PageIndexChanging" FooterStyle Font-Bold="true" BackColor="#FEFBFC" ForeColor="black" <columns><asp:TemplateField  HeaderText="Total"><itemtemplate>
<asp:Label ID="lbltotalamtachieved" runat="server" Text='<%#Eval("Total_AmtAchieved") %>'/>
</itemtemplate>
<footertemplate>
<asp:Label ID="lblfoottotal" runat="server"  Text='<%# Eval("AchievedPer")%>'/>
</footertemplate>

<asp:TemplateField HeaderText="Profit">
<itemtemplate><asp:Label ID="lblprofit" runat="server" Text='<%# Eval("Profit") %>'/>
</itemtemplate><footertemplate><asp:Label ID="lblfootprofit"runat="server" Text='<%# Eval("AchievedProfit")%>'/>



//page.cs


C#
protected void gvProd_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        decimal total = 0;
        decimal profit = 0;

        //Check if the current row is datarow or not
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //Add the value of column
            total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "AchievedPer"));
            profit += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "AchievedProfit"));

        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            //Find the control label in footer
            Label lblamount = (Label)e.Row.FindControl("lblfoottotal");
            //Assign the total value to footer label control
            lblamount.Text = "Total is : " + total.ToString();
            //Find the control label in footer
            Label lblprofit = (Label)e.Row.FindControl("lblfootprofit");
            //Assign the total value to footer label control
            lblprofit.Text = "Profit is : " + profit.ToString();
        }
    }



[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 21-Mar-14 1:37am
v3
Comments
nandakishoreroyal 21-Mar-14 7:41am    
what values you are getting in total and profit???
Gladiator3 22-Mar-14 3:14am    
say for example I gave value for total=20000.0
profit=5000.0
And I'm able to get my values inside this code:

if (e.Row.RowType == DataControlRowType.DataRow)
{
//Add the value of column
total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "AchievedPer"));//20000.0
profit += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "AchievedProfit"));//5000.0

}
$*Developer - Vaibhav*$ 21-Mar-14 8:28am    
you want to show grid view column total in the label?

1 solution

you have declared at Footer Template. So,you cant get the value at DataRow

C#
if (e.Row.RowType == DataControlRowType.DataRow)
      {
          //Add the value of column
          total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "AchievedPer"));
          profit += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "AchievedProfit"));

      }


C#
if (e.Row.RowType == DataControlRowType.Footer)
      {
          //Add the value of column
          total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "AchievedPer"));
          profit += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "AchievedProfit"));

      }
 
Share this answer
 
Comments
JoCodes 21-Mar-14 9:06am    
Nice Catch
King Fisher 21-Mar-14 9:10am    
thank you :)
Gladiator3 22-Mar-14 3:17am    
Hi,
I'm able to get values only in this code
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Add the value of column
total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "AchievedPer"));
profit += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "AchievedProfit"));

}
but when give as below:

if (e.Row.RowType == DataControlRowType.Footer)
{
//Add the value of column
total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "AchievedPer"));
profit += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "AchievedProfit"));

}
//total value returns 0.0 and profit also returns 0.0
//I want display values from my query. In my query I'm able to get the values,also in my datatable too, while debugging,but, can't understand, why it is not being passed in my footer? Should I take care any other logic?
King Fisher 22-Mar-14 3:36am    
if (e.Row.RowType == DataControlRowType.Footer)
{

Label lblamount = (Label)e.Row.FindControl("lblfoottotal");

Label lblprofit = (Label)e.Row.FindControl("lblfootprofit");
total += Convert.ToInt32(lblamount.Text);
profit +=Convert.ToInt32(lblprofit .Text);


}
lblamount.Text = "Total is : " + total.ToString();
lblprofit.Text = "Profit is : " + profit.ToString();
Gladiator3 22-Mar-14 6:39am    
Hi,
In this code


protected void gvProd_RowDataBound(object sender, GridViewRowEventArgs e)
{
decimal total = 0;
decimal profit = 0;



//Check if the current row is datarow or not
if (e.Row.RowType == DataControlRowType.DataRow)
{
total = Convert.ToDecimal(((DataRowView)e.Row.DataItem)["AchievedPer"].ToString());//got the expected result
profit = Convert.ToDecimal(((DataRowView)e.Row.DataItem)["AchievedProfit"].ToString());//got the expected result
//Add the value of column
//total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "AchievedPer"));
//profit += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "AchievedProfit"));

}
if (e.Row.RowType == DataControlRowType.Footer)
{
//Find the control label in footer
Label lbltotal = (Label)e.Row.FindControl("lblfoottotal");//Rs. 0.00
Label lblprofit = (Label)e.Row.FindControl("lblfootprofit");//Rs. 0.00
//Assign the total value to footer label control
// lbltotal.Text = total.ToString();
lbltotal.Text = String.Format("{0:C}", total);//Rs. 0.00

//Assign the total value to footer label control
// lblprofit.Text = profit.ToString();
lblprofit.Text = String.Format("{0:C}", profit);//Rs. 0.00
}

}


Why am I not able to get the expected result in my assigned my decimal 'total' and 'profit' variable?
please monitor the code closely and let me know the exact solution.

Since,I tried you method and got more errors.

Please guide me.

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