user3985746
user3985746

Reputation:

How can I get the sum total of a column in the gridview to a textbox outside the gridview?

In asp.net C#, How can I get the sum total of a column in the gridview to a textbox outside the gridview?

Note: Gridview data is not coming from any database it is being entered manually by the user.

Upvotes: 2

Views: 13974

Answers (3)

Rohit
Rohit

Reputation: 1550

This should do the work.

int total = 0;
for (int row = 0; row  < GridView1.Rows.Count; row ++)
{
    for (int col = 0; col < GridView1.Columns.Count; col++) 
         total +=Convert.ToInt32( GridView1.Rows[row].Cells[col].Text);
}

Upvotes: 0

ALOK
ALOK

Reputation: 553

Call this Method on some button click or on new row creation or whatever event you want to

    private void GrandTotal()
    {
     float GTotal = 0f;
     for (int i = 0; i < GridView1.Rows.Count; i++)
     {
        String total = (GridView1.Rows[i].FindControl("lblTotal") as Label).Text;
        GTotal += Convert.ToSingle(total);
     }
     txtTotal.text=GTotal.toString();
    }

Hope i helped :D

Upvotes: 3

Nima Derakhshanjan
Nima Derakhshanjan

Reputation: 1404

try this

int sum = 0;

for (int i = 0; i < GridView1.Rows.Count; i++)
{
    sum +=Convert.ToInt32( GridView1.Rows[i].Cells[/*the column index*/].Text);
}

Upvotes: 0

Related Questions