Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
my grid view is like


total----balance
10 ----- 10
20 ----- 30
5 - ----- 35


the total column is fetched from data base by some conditions and i want to generate balance column at run time as above
any one can explain i m just biggener
Posted

Ok, for a beginner, here is the direction:
You need to use gridview's RowDataBound method. This will be called for row databind to grid. In this method, find the value of total for that row and add to a global counter. Assign the current total to the balance field of that row. Global counter will keep track of sum till that row bind giving you balance sum as you need.

Try now. If needed, read about RowDataBound and see how it works.
 
Share this answer
 
Comments
sandeep nagabhairava 19-Jul-12 6:09am    
well sandeep, most of the people just write the code or past some links for output. but your something special.keep it up... my 5!
hi,

Add the column balance and below code in RowDataBound Event of the GridView
C#
//if bound column
if(e.Row.RowType==DataControlRowType.DataRow)
{
    if(e.Row.RowIndex==0)
    {
        e.Row.Cells[7].Text=e.Row.Cells[6].Text;
    }
    else
    {
        e.Row.Cells[7].Text = Convert.ToInt32(GridView1.Rows[e.Row.RowIndex - 1].Cells[7].Text) + Convert.ToInt32( e.Row.Cells[6].Text);
    }

}

//if template column

Label lbl = e.Row.FindControl("lblBalance") as Label;
if(e.Row.RowType==DataControlRowType.DataRow)
{
    if(e.Row.RowIndex==0)
    {
        lbl.Text=e.Row.Cells[6].Text;
    }
    else
    {
        lbl= Convert.ToInt32(((Label)GridView1.Rows[e.Row.RowIndex - 1].FindControl("lblBalance")).Text) + Convert.ToInt32( e.Row.Cells[6].Text);
    }

}


hope it helps.
 
Share this answer
 
v3
Comments
Pradeep_kaushik 19-Jul-12 6:45am    
its not working
Karthik Harve 19-Jul-12 6:54am    
what is the problem you are facing? show your code.
Pradeep_kaushik 19-Jul-12 7:02am    
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[7].Text = (Convert.ToInt32(GridView1.Rows[e.Row.RowIndex - 1].Cells[6].Text) + Convert.ToInt32(e.Row.Cells[6].Text)).ToString();
}
}
Karthik Harve 19-Jul-12 7:11am    
check my updated solution
Pradeep_kaushik 19-Jul-12 7:07am    
i have to generate a bill in a project
in my data base there is no column of named balance total column is there
but i want that at run time by adding values of total column as i explain in example
add the template column in the gridview source along with other columns. take care about the column indexes.

ASP.NET
<asp:gridview id="GridView1" runat="server">
   <columns>
       <asp:templatecolumn headertext="Balance">
             <itemtemplate>
                  <asp:label id="lblBalance" runat="server" text="" />
             </itemtemplate>
       </asp:templatecolumn>
   </columns>
</asp:gridview>


check my updated answer for template column in Solution 2.
 
Share this answer
 
v3
Comments
Pradeep_kaushik 21-Jul-12 3:31am    
sir you giving this code for VS2010 or VS 2008

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