Search This Blog

Monday, October 4, 2010

Calculating total of template textbox inside gridview in row wise.

 In some scenario we need to get the total for the gridview rowwise.
That means if we need to calculate the marklist, timesheet and any other row wise calculation we can do it using javascript with simple gridview's event.

 Javascript to calculate the row wise total:


<script language="javascript" type="text/javascript">
    function updateValue(theGrid, rowIdx)
    {
        var total = 0;
        var cellCount = GridView1.rows[rowIdx].cells.length - 2;
        for(var i=1; i<cellCount; i++)
        {
            if(GridView1.rows[rowIdx].cells[i].children[0].children[0].value != "")
                total += (GridView1.rows[rowIdx].cells[i].children[0].children[0].value - 0);
        }
        GridView1.rows[rowIdx].cells[8].children[0].children[0].value =  total;
    }
</script>

 Add this in the code behind with the gridview's rowdatabound event.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int rowIndex = Convert.ToInt32(e.Row.DataItemIndex) + 1;
            for (int i = 1; i < e.Row.Cells.Count - 1; i++)
            {
                evtHandler = "updateValue(" + GridView1.ClientID + "," + rowIndex + ")";
                ((TextBox)e.Row.FindControl("TextBox" + i.ToString())).Attributes.Add("onblur", evtHandler);
            }
        }
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.