Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
How to get total of gridview row values asp.net,

I Want to gat each of row total in my gridview.

I try this code it's not working

C#
protected void TAXGridview0_RowCreated(object sender, GridViewRowEventArgs e)
       {

        if (e.Row.RowType == DataControlRowType.DataRow)
          {
             int total = Convert.ToInt32(e.Row.Cells[3].Text) +                  Convert.ToInt32(e.Row.Cells[4].Text) + Convert.ToInt32(e.Row.Cells[5].Text) + Convert.ToInt32(e.Row.Cells[6].Text) + Convert.ToInt32(e.Row.Cells[7].Text) + Convert.ToInt32(e.Row.Cells[8].Text) + Convert.ToInt32(e.Row.Cells[9].Text);

            ((Label)TAXGridview.FindControl("Label1")).Text = Convert.ToString(total);

           }

       }



any one can help me,Thank you

no anser y am I wrong,
Posted
Updated 1-Oct-13 20:11pm
v2

Since you are not adding value to total for every row, instead you are just doing the add of the one row thats why the result you are getting is of final row only.

Please look the below code that will help you.

C#
decimal sumFooterValue = 0;
  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
         string sponsorBonus = ((Label)e.Row.FindControl("Label2")).Text;
         string pairingBonus = ((Label)e.Row.FindControl("Label3")).Text;
         string staticBonus = ((Label)e.Row.FindControl("Label4")).Text;
         string leftBonus = ((Label)e.Row.FindControl("Label5")).Text;
         string rightBonus = ((Label)e.Row.FindControl("Label6")).Text;
         decimal totalvalue = Convert.ToDecimal(sponsorBonus) + Convert.ToDecimal(pairingBonus) + Convert.ToDecimal(staticBonus) + Convert.ToDecimal(leftBonus) + Convert.ToDecimal(rightBonus);
         e.Row.Cells[6].Text = totalvalue.ToString();
        sumFooterValue += totalvalue
        }

    if (e.Row.RowType == DataControlRowType.Footer)
        {
           Label lbl = (Label)e.Row.FindControl("lblTotal");
           lbl.Text = sumFooterValue.ToString();
        }

   }
 
Share this answer
 
Comments
[no name] 2-Oct-13 3:39am    
Thanx i try this code
[no name] 2-Oct-13 3:49am    
faceing This error.how to fix.
Object reference not set to an instance of an object.
[no name] 2-Oct-13 7:10am    
May be any of the object you are using is nor initialize properly. Can you ecplain exactly on which object you are getting this error.

-SG
[no name] 3-Oct-13 2:12am    
This is my Code

protected void btn_Cash_Ok_Click(object sender, EventArgs e)
{
try
{
TAXGridview0.Visible = true;
TAXGridview.Visible = false;





for (int i = 0; i < TAXGridview.Rows.Count; i++)
{
if ((TAXGridview.Rows[i].Cells[10].FindControl("CB_CHECK") as CheckBox).Checked == true)
{




if (Session["dt1"] != null) dt1 = (DataTable)Session["dt1"];
DataRow dr = dt1.NewRow();
dr["HAWB"] = TAXGridview.Rows[i].Cells[1].Text;//1
dr["Value"] = TAXGridview.Rows[i].Cells[2].Text;//2

TextBox Duty = (TextBox)TAXGridview.Rows[i].Cells[3].FindControl("txtduty"); //Just change the index of the Cells to which your TextBox resides in the column
dr["Duty"] = Convert.ToDouble(Duty.Text);//3

TextBox PAL = (TextBox)TAXGridview.Rows[i].Cells[4].FindControl("txtPAL");
dr["PAL"] = Convert.ToDouble(PAL.Text);//4

TextBox VAT = (TextBox)TAXGridview.Rows[i].Cells[5].FindControl("txtVat");
dr["VAT"] = Convert.ToDouble(VAT.Text);//5

TextBox UHDAmt = (TextBox)TAXGridview.Rows[i].Cells[6].FindControl("txtUHD");
dr["UHDAmt"] = Convert.ToDouble(UHDAmt.Text);//6

TextBox SUR = (TextBox)TAXGridview.Rows[i].Cells[7].FindControl("txtSUR");
dr["SUR"] = Convert.ToDouble(SUR.Text);//7

TextBox EIC = (TextBox)TAXGridview.Rows[i].Cells[8].FindControl("txtEIC");
dr["EIC"] = Convert.ToDouble(EIC.Text);//8

TextBox SRL = (TextBox)TAXGridview.Rows[i].Cells[9].FindControl("txtSRL");
dr["SRL"] = Convert.ToDouble(SRL.Text);//9







dt1.Rows.Add(dr);
TAXGridview0.DataSource = dt1;
TAXGridview0.DataBind();
}


protected void TAXGridview0_RowDataBound(object sender, GridViewRowEventArgs e)
{

try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
String sponsorBonus = ((TextBox)e.Row.FindControl("txtduty")).Text;
String pairingBonus = ((TextBox)e.Row.FindControl("txtPAL")).Text;
String staticBonus = ((TextBox)e.Row.FindControl("txtVat")).Text;
String leftBonus = ((TextBox)e.Row.FindControl("txtUHD")).Text;
String rightBonus = ((TextBox)e.Row.FindControl("txtSUR")).Text;
decimal totalvalue = Convert.ToDecimal(sponsorBonus) + Convert.ToDecimal(pairingBonus) + Convert.ToDecimal(staticBonus) + Convert.ToDecimal(leftBonus) + Convert.ToDecimal(rightBonus);
e.Row.Cells[10].Text = totalvalue.ToString();
sumFooterValue += totalvalue;
}

if (e.Row.RowType == DataControlRowType.Footer)
{
Label lbl = (Label)e.Row.FindControl("lblTotal");
lbl.Text = sumFooterValue.ToString();
}



}
SQL
I suggest you to add one more column in your gridveiw Namely Total

then write the code in gridveiw DataBound event like

int total=0;
for (int i=0;i<grid.Rows.Count)
{
   for(int j=o;j<grid.Columns.Count)
   {
      total=total+int.Parse(grid.Rows[i].Cell[j].Text);
      
   }
grid.Rows[i].Cell["Total"].Text=total;
}


then fetch the value from grid and display where you want
 
Share this answer
 
v2
Hi,

Below code will help you to show the total of each cell in a row as well as the total of all the rows.

1. Add a column in your gridveiw Namely Sum where you can display the sum of other cells of the row

XML
<asp:TemplateField HeaderText="Sum">
<ItemTemplate><asp:Label ID="Label1" runat="server" /></ItemTemplate>
<FooterTemplate><asp:Label ID="Label2" runat="server" /></FooterTemplate>
</asp:TemplateField>


2. Write the below code in gridveiw DataBound event like

int sumFooterValue = 0;
    protected void TAXGridview_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // to get total of each cell in the current row
int total = Convert.ToInt32(e.Row.Cells[3].Text) + Convert.ToInt32(e.Row.Cells[4].Text) + Convert.ToInt32(e.Row.Cells[5].Text) + Convert.ToInt32(e.Row.Cells[6].Text) + Convert.ToInt32(e.Row.Cells[7].Text) + Convert.ToInt32(e.Row.Cells[8].Text) + Convert.ToInt32(e.Row.Cells[9].Text);
              
Label RowTotal = (Label)e.Row.FindControl("Label1");
              RowTotal.Text = total.ToString();

            //to calculate some of all rows
            sumFooterValue += total;
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            //to show sum of all the rows in footer
            Label Total = (Label)e.Row.FindControl("Label2");
            Total.Text = "Total = " + sumFooterValue.ToString();
        }
    }


Hope this will help.
 
Share this answer
 
Label.Text=Gridview.Rows.Count;
 
Share this answer
 
 
Share this answer
 
v4
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
int total=0;
for(int i=0;i<GridView1.Rows.Count;i++)
{
for(int j=0;j<GridView1.Cloumns.Count;j++)
{
total=total+int.Parse(GridView1.Rows[i].Cells[j].Text);
}
}


}
 
Share this answer
 
C#
protected void TAXGridview0_RowDataBound(object sender, GridViewRowEventArgs e)
      {
          //if header column we dont need multiplication
          if (e.Row.RowIndex != -1)
          {
              //taking values from first cell. my first cell contain value, you can change
              string id = e.Row.Cells[3].Text;
              //taking values from second cell. my second cell contain value, you can change
              string id2 = e.Row.Cells[4].Text;
              //multiplication
              double mult = double.Parse(id) + double.Parse(id2);
              //adding result to last column. coz we add new column in last.
              e.Row.Cells[e.Row.Cells.Count - 1].Text = mult.ToString();
          }



      }

    protected void TAXGridview0_RowCreated(object sender, GridViewRowEventArgs e)
    {
        //create a instance of table cell
        // this table cell is going to add gridview as new column
        TableCell tc = new TableCell();
        //first cell must be -1 ie the header of gridview. if it header
        if (e.Row.RowIndex == -1)
        {
            //add header column name. you can add more styles in this section
            tc.Text = "Total";
            //add style for header column
            tc.Style.Add(HtmlTextWriterStyle.FontWeight, "bold");
        }
        //cell add to gridview row
        e.Row.Cells.Add(tc);
    }
 
Share this answer
 

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