# Calculating total in a column on a GridView after a value is entered in templatefield text boxes. • ### Question

• User2028823583 posted

Hi there.  I've got a ASP.NET Web page in C# that has a gridview in it with 10 templatefield text boxes.  Then, on the far right, is column #11,(total label), which is also a template field.

I'm curious, if there is a way to calculate the sum of the first 10 textboxes (by row) and display it in the total label after a user enters a value in each of the 10 textboxes.  So, it'd kind of look like a running total...

Wednesday, November 14, 2018 3:13 PM

### All replies

• User475983607 posted

I'm curious, if there is a way to calculate the sum of the first 10 textboxes (by row) and display it in the total label after a user enters a value in each of the 10 textboxes.  So, it'd kind of look like a running total...

Write a JavaScript/jQuery application.

The main programming problem is selecting the inputs within a row on an event like an input losing focus.  I generally come up with an HTML design that makes selecting a row and the TDs within the row simple.

Is there anyway you can show us the code that you have attempted?  Explain the expected outcome and the actual outcome?

Wednesday, November 14, 2018 3:36 PM
• User-893317190 posted

Hi funluckykitty,

You could use jquery to meet your requirement.

Below is my code.

```  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">

<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox runat="server" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox runat="server" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox runat="server" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox runat="server" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox runat="server" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
total: <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

<script>
\$(function () {

\$("#GridView1 input[type='text']").keyup(function () {// register keyup event on all the textbox
var total = 0;     // get all the textbox in the same row and calculate their tatoal value
\$(this).parent().parent().find("input[type='text']").each(function (index,ele) {

total += isNaN(parseInt(\$(ele).val())) ? 0 : parseFloat(\$(ele).val());
})
\$(this).parent().parent().find("[id*=Label1]").text(total);//set the label's text to total
})

})
</script>
```

The result. Best regards,

Ackerly Xu

Thursday, November 15, 2018 4:32 AM
{"ft_lang_label":"__label__en","ft_lang_prob":0.9030408,"math_prob":0.65104765,"size":610,"snap":"2022-27-2022-33","text_gpt3_token_len":137,"char_repetition_ratio":0.0990099,"word_repetition_ratio":0.0,"special_character_ratio":0.22950819,"punctuation_ratio":0.088709675,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9618259,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-06-29T17:04:19Z\",\"WARC-Record-ID\":\"<urn:uuid:6b9dc321-898f-44fc-9327-d6af7f398e4f>\",\"Content-Length\":\"61428\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:da9f946c-5875-4d5e-b804-715ef2443129>\",\"WARC-Concurrent-To\":\"<urn:uuid:9365c452-3a0c-4e3d-a184-cad63cf6c042>\",\"WARC-IP-Address\":\"13.107.213.40\",\"WARC-Target-URI\":\"https://social.msdn.microsoft.com/Forums/en-US/52307bd2-623b-4e96-8551-adff41e2246e/calculating-total-in-a-column-on-a-gridview-after-a-value-is-entered-in-templatefield-text-boxes?forum=aspgettingstarted\",\"WARC-Payload-Digest\":\"sha1:WIH5PCIWJLCY64HZVXWXVPOBO3XP62H3\",\"WARC-Block-Digest\":\"sha1:SNBBZIZM74SEZOOKWEXBHF3DHGPFIN2K\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656103640328.37_warc_CC-MAIN-20220629150145-20220629180145-00721.warc.gz\"}"}