Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
hi how to calculate cummulative frequency in gridview ? using column values of gridview col1 cf 2 2 3 5 4 8 5 13 like this. the user will enter only the first col values.. how to calculate second column values using gridview. my coding is
string  pr1 = gv.Rows[0].Cells[4].ToString();
    //int pr = System.Convert.ToInt32(pr1);
    int rw1;
    for (int ri =0; ri <= gv.Rows.Count - 2; ri++)
    {

        GridViewRow rw = gv.Rows[ri];
        GridViewRow prrw = gv.Rows[ri+1];
        //for (int i = 1; i < rw.Cells.Count; i++)
        //{
            prrw.Cells[5].Text = pr1;
            rw1 = System.Convert.ToInt32(rw.Cells[4].Text) + System.Convert.ToInt32(prrw.Cells[4].Text);
            pr1 = rw1.ToString ();
    }

but the output was displaying

col1   cf
2
3   System.Web.UI.WebControls.DataControlFieldCell
4   5
6   7
8   10
Posted
Updated 8-Feb-11 20:35pm
v6
Comments
Albin Abel 9-Feb-11 0:23am    
Please explain more. Are you asking about calculate cumulative frequency of values in a single column?
gowdhami 9-Feb-11 0:27am    
yes i have to calculate cf for single column values and it should be display in another column
Albin Abel 9-Feb-11 1:25am    
just loop through the gridview rows, calculate the cumulative and update the cf column. Is that way you don't want? or you are looking for the exact coding?. What are those gridview field types?

1 solution

Hi
Assuming you have both columns are databound. I have the aspx code like this

XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    DataSourceID="SqlDataSource1"
     OnDataBound="GridView1_OnDatabound">
    <Columns>
        <asp:BoundField DataField="col1" HeaderText="col1" SortExpression="col1" />
        <asp:BoundField DataField="cf" HeaderText="cf" SortExpression="cf" />
    </Columns>
</asp:GridView>


onDatabound event I am calculating the cf and populate as follows. Again assuming the col1 is the first column and cf is the second one, otherwise you need to change the index position as per your gridview

C#
protected void GridView1_OnDatabound(object sender, EventArgs e)
{
    int cf =0;
    int currentValue = 0;
    foreach (GridViewRow row in GridView1.Rows)
    {
        currentValue = Convert.ToInt32(row.Cells[0].Text);
        cf += currentValue;
        row.Cells[1].Text = cf.ToString();
    }
}


Let me know if that works!
 
Share this answer
 
Comments
gowdhami 9-Feb-11 3:16am    
getting error in currentValue = Convert.ToInt32(row.Cells[0].Text); Input string was not in a correct format.
gowdhami 9-Feb-11 3:24am    
hey ur coding is working well in button click event.. thank you

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