ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Calculate percentage on ASP.Net GridView RowUpdating event using C# and VB.Net<p>Hi <a class="username" rel="VasuDev">VasuDev</a>,</p> <p>Refer below sample and link -</p> <h1 class="header"><a title="Step by Step Tutorial for performing Insert Edit Update and Delete in GridView in ASP.Net'a" href="https://www.aspsnippets.com/Articles/Step-by-Step-Tutorial-for-performing-Insert-Edit-Update-and-Delete-in-GridView-in-ASPNet.aspx">Step by Step Tutorial for performing Insert Edit Update and Delete in GridView in ASP.Net</a></h1> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:GridView ID=&#34;dgCptResult&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;False&#34; OnRowEditing=&#34;dgCptResult_RowEditing&#34; OnPageIndexChanging=&#34;dgCptResult_PageIndexChanging1&#34; AllowPaging=&#34;True&#34; OnRowCancelingEdit=&#34;dgCptResult_RowCancelingEdit1&#34; OnRowUpdating=&#34;dgCptResult_RowUpdating1&#34; DataKeyNames=&#34;HQPHTBSYSID&#34; AutoGenerateEditButton=&#34;True&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;HQPHTBSYSID&#34; HeaderText=&#34;HQPID&#34; ReadOnly=&#34;True&#34; /&gt; &lt;asp:BoundField DataField=&#34;NAME&#34; HeaderText=&#34;NAME&#34; ReadOnly=&#34;True&#34; /&gt; &lt;asp:TemplateField HeaderText=&#34;CORRECT&#34;&gt; &lt;EditItemTemplate&gt; &lt;asp:TextBox ID=&#34;txtcorrect&#34; runat=&#34;server&#34;&gt;&lt;/asp:TextBox&gt; &lt;/EditItemTemplate&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;lblcorrect&#34; runat=&#34;server&#34;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;INCORRECT&#34;&gt; &lt;EditItemTemplate&gt; &lt;asp:TextBox ID=&#34;txtincorrect&#34; runat=&#34;server&#34;&gt;&lt;/asp:TextBox&gt; &lt;/EditItemTemplate&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;lblincorrect&#34; runat=&#34;server&#34;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;ATTENDED&#34;&gt; &lt;EditItemTemplate&gt; &lt;asp:TextBox ID=&#34;txtAtended&#34; runat=&#34;server&#34;&gt;&lt;/asp:TextBox&gt; &lt;/EditItemTemplate&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;lblAtended&#34; runat=&#34;server&#34;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;UNATTENDED&#34;&gt; &lt;EditItemTemplate&gt; &lt;asp:TextBox ID=&#34;txtUnAtended&#34; runat=&#34;server&#34;&gt;&lt;/asp:TextBox&gt; &lt;/EditItemTemplate&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;lblUnAtended&#34; runat=&#34;server&#34;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;RESULT&#34;&gt; &lt;EditItemTemplate&gt; &lt;asp:TextBox ID=&#34;txtresult&#34; runat=&#34;server&#34;&gt;&lt;/asp:TextBox&gt; &lt;/EditItemTemplate&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;lblresult&#34; runat=&#34;server&#34;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;HeaderStyle BackColor=&#34;#C0C0FF&#34; /&gt; &lt;/asp:GridView&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { BindGrid(); } } private void BindGrid() { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { new DataColumn(&#34;HQPHTBSYSID&#34;, typeof(int)), new DataColumn(&#34;NAME&#34;, typeof(string)) }); dt.Rows.Add(1, &#34;Mudassar Khan&#34;); dt.Rows.Add(2, &#34;John Hammond&#34;); dgCptResult.DataSource = dt; dgCptResult.DataBind(); } protected void dgCptResult_PageIndexChanging1(object sender, GridViewPageEventArgs e) { dgCptResult.PageIndex = e.NewPageIndex; dgCptResult.DataBind(); } protected void dgCptResult_RowCancelingEdit1(object sender, GridViewCancelEditEventArgs e) { dgCptResult.EditIndex = -1; this.BindGrid(); } protected void dgCptResult_RowEditing(object sender, GridViewEditEventArgs e) { dgCptResult.EditIndex = e.NewEditIndex; this.BindGrid(); } protected void dgCptResult_RowUpdating1(object sender, GridViewUpdateEventArgs e) { GridViewRow row = dgCptResult.Rows[e.RowIndex]; int Id = Convert.ToInt32(dgCptResult.DataKeys[e.RowIndex].Values[0]); int correct = (Convert.ToInt32((row.FindControl(&#34;txtcorrect&#34;) as TextBox).Text)); int inCorrect = (Convert.ToInt32((row.FindControl(&#34;txtincorrect&#34;) as TextBox).Text)); int attend = (Convert.ToInt32((row.FindControl(&#34;txtAtended&#34;) as TextBox).Text)); int unatttend = (Convert.ToInt32((row.FindControl(&#34;txtUnAtended&#34;) as TextBox).Text)); int total = attend + unatttend; int result = (correct * 100) / total; (row.FindControl(&#34;txtresult&#34;) as TextBox).Text = result.ToString() + &#34;%&#34;; }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not Me.IsPostBack Then BindGrid() End If End Sub Private Sub BindGrid() Dim dt As DataTable = New DataTable() dt.Columns.AddRange(New DataColumn() {New DataColumn(&#34;HQPHTBSYSID&#34;, GetType(Integer)), New DataColumn(&#34;NAME&#34;, GetType(String))}) dt.Rows.Add(1, &#34;Mudassar Khan&#34;) dt.Rows.Add(2, &#34;John Hammond&#34;) dgCptResult.DataSource = dt dgCptResult.DataBind() End Sub Protected Sub dgCptResult_PageIndexChanging1(ByVal sender As Object, ByVal e As GridViewPageEventArgs) dgCptResult.PageIndex = e.NewPageIndex dgCptResult.DataBind() End Sub Protected Sub dgCptResult_RowCancelingEdit1(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs) dgCptResult.EditIndex = -1 Me.BindGrid() End Sub Protected Sub dgCptResult_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs) dgCptResult.EditIndex = e.NewEditIndex Me.BindGrid() End Sub Protected Sub dgCptResult_RowUpdating1(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) Dim row As GridViewRow = dgCptResult.Rows(e.RowIndex) Dim Id As Integer = Convert.ToInt32(dgCptResult.DataKeys(e.RowIndex).Values(0)) Dim correct As Integer = (Convert.ToInt32((TryCast(row.FindControl(&#34;txtcorrect&#34;), TextBox)).Text)) Dim inCorrect As Integer = (Convert.ToInt32((TryCast(row.FindControl(&#34;txtincorrect&#34;), TextBox)).Text)) Dim attend As Integer = (Convert.ToInt32((TryCast(row.FindControl(&#34;txtAtended&#34;), TextBox)).Text)) Dim unatttend As Integer = (Convert.ToInt32((TryCast(row.FindControl(&#34;txtUnAtended&#34;), TextBox)).Text)) Dim total As Integer = attend + unatttend Dim result As Integer = (correct * 100) / total TryCast(row.FindControl(&#34;txtresult&#34;), TextBox).Text = result.ToString() &amp; &#34;%&#34; End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/hi6uvaf.gif" alt="" width="472" height="155" /></p>https://www.aspforums.net:443/Threads/172478/Calculate-percentage-on-ASPNet-GridView-RowUpdating-event-using-C-and-VBNet/https://www.aspforums.net:443/Threads/172478/Calculate-percentage-on-ASPNet-GridView-RowUpdating-event-using-C-and-VBNet/Thu, 18 Jul 2019 08:11:56 GMT