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.Update Checked (Selected) GridView Rows based on Id using C# in ASP.Net<p>Hi <a class="username" rel="VinitaAkha"> VinitaAkha</a>,</p> <p>Refer below code.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;script type=&#34;text/javascript&#34; src=&#34;http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; $(function () { $(&#39;[id*=chk]&#39;).on(&#39;change&#39;, function () { var value = 0; $(&#39;[id*=chk]:checked&#39;).each(function () { var row = $(this).closest(&#39;tr&#39;); value = value + parseInt(row.find(&#39;[id*=Total]&#39;).html()); }); $(&#34;[id*=gvData] [id*=lblGrandTotal]&#34;).html(value); $(&#34;[id*=gvSelected] [id*=lblGrandTotal]&#34;).html(value); }); }); &lt;/script&gt; &lt;asp:Label ID=&#34;lbl_app&#34; runat=&#34;server&#34;&gt;&lt;/asp:Label&gt; &lt;asp:GridView ID=&#34;gvData&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;False&#34; ShowFooter=&#34;True&#34; GridLines=&#34;None&#34;&gt; &lt;Columns&gt; &lt;asp:TemplateField HeaderText=&#34;Select Farmer&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:CheckBox ID=&#34;chk&#34; runat=&#34;server&#34; AutoPostBack=&#34;true&#34; OnCheckedChanged=&#34;Transfer&#34; /&gt; &lt;/ItemTemplate&gt; &lt;ItemStyle HorizontalAlign=&#34;Center&#34; /&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Farmer Id&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Id&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Id&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Farmer Name&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Name&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Name&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Farmer Email&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Email&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Email&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Farmer State&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;State&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;State&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Farmer District&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;District&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;District&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Farmer Address&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Address&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Address&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Farmer Contact&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Contact&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Contact&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Company Potato Quantity&#34; Visible=&#34;false&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;PQuantity&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;PQuantity&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Farmer Quantity&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Quantity&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Quantity&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Farmer Total&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Total&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Total&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;center&gt; &lt;asp:Button ID=&#34;Button1&#34; runat=&#34;server&#34; Text=&#34;Add&#34; ForeColor=&#34;Red&#34; OnClick=&#34;Button1_Click&#34; Font-Size=&#34;20px&#34; CssClass=&#34;bg-primary&#34; Width=&#34;100px&#34; Height=&#34;50px&#34; /&gt; &lt;/center&gt; &lt;hr /&gt; &lt;center&gt; &lt;u style=&#34;font-size: 30px&#34;&gt;Selected Rows&lt;/u&gt;&lt;/center&gt; &lt;br /&gt; &lt;asp:GridView ID=&#34;gvSelected&#34; runat=&#34;server&#34; ShowFooter=&#34;true&#34; GridLines=&#34;None&#34; AutoGenerateColumns=&#34;false&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;Name&#34; HeaderText=&#34;Name&#34; /&gt; &lt;asp:BoundField DataField=&#34;State&#34; HeaderText=&#34;State&#34; /&gt; &lt;asp:BoundField DataField=&#34;District&#34; HeaderText=&#34;District&#34; /&gt; &lt;asp:BoundField DataField=&#34;Address&#34; HeaderText=&#34;Address&#34; /&gt; &lt;asp:BoundField DataField=&#34;Email&#34; HeaderText=&#34;Email&#34; /&gt; &lt;asp:BoundField DataField=&#34;Contact&#34; HeaderText=&#34;Contact&#34; /&gt; &lt;asp:BoundField DataField=&#34;Quantity&#34; HeaderText=&#34;Quantity&#34; /&gt; &lt;asp:BoundField DataField=&#34;PQuantity&#34; HeaderText=&#34;Potato Quantity&#34; /&gt; &lt;asp:TemplateField HeaderText=&#34;Total&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;Total&#34; runat=&#34;server&#34; Text=&#39;&lt;%# Eval(&#34;Total&#34;) %&gt;&#39;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;FooterTemplate&gt; Total : &lt;asp:Label ID=&#34;lblGrandTotal&#34; runat=&#34;server&#34; /&gt; &lt;/FooterTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <pre class="brush: csharp">using System.Data; using System.Data.SqlClient;</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { new DataColumn(&#34;Id&#34;, typeof(string)), new DataColumn(&#34;Role&#34;, typeof(string)), new DataColumn(&#34;Email&#34;, typeof(string)), new DataColumn(&#34;Contact&#34;,typeof(string)), new DataColumn(&#34;State&#34;, typeof(string)), new DataColumn(&#34;District&#34;, typeof(string)), new DataColumn(&#34;Address&#34;,typeof(string)), new DataColumn(&#34;Name&#34;, typeof(string)), new DataColumn(&#34;Quantity&#34;, typeof(int)), new DataColumn(&#34;PQuantity&#34;,typeof(int)), new DataColumn(&#34;Total&#34;,typeof(int))}); dt.Rows.Add(1, &#34;Role 1&#34;, &#34;Email 1&#34;, &#34;Contact 1&#34;, &#34;State 1&#34;, &#34;District 1&#34;, &#34;Address 1&#34;, &#34;Name 1&#34;, 10, 5, 50); dt.Rows.Add(2, &#34;Role 2&#34;, &#34;Email 2&#34;, &#34;Contact 2&#34;, &#34;State 2&#34;, &#34;District 2&#34;, &#34;Address 2&#34;, &#34;Name 2&#34;, 20, 5, 100); dt.Rows.Add(3, &#34;Role 3&#34;, &#34;Email 3&#34;, &#34;Contact 3&#34;, &#34;State 3&#34;, &#34;District 3&#34;, &#34;Address 3&#34;, &#34;Name 3&#34;, 15, 5, 75); dt.Rows.Add(4, &#34;Role 4&#34;, &#34;Email 4&#34;, &#34;Contact 4&#34;, &#34;State 4&#34;, &#34;District 4&#34;, &#34;Address 4&#34;, &#34;Name 4&#34;, 12, 5, 60); gvData.DataSource = dt; gvData.DataBind(); } } protected void Button1_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection(); SqlCommand cmd; foreach (GridViewRow row in gvData.Rows) { CheckBox chkRow = (row.Cells[0].FindControl(&#34;chk&#34;) as CheckBox); if (chkRow.Checked) { con.Open(); try { int id = Convert.ToInt16((row.FindControl(&#34;Id&#34;) as Label).Text); cmd = new SqlCommand(&#34;UPDATE AddRaw_199 SET status = 1 WHERE Id= @id&#34;, con); cmd.Parameters.AddWithValue(&#34;@id&#34;, id); int suc = cmd.ExecuteNonQuery(); } catch { } finally { if (con.State == ConnectionState.Open) { con.Close(); } } } } lbl_app.CssClass = &#34;text-center text-success&#34;; lbl_app.Text = &#34;Status Updated Successfully&#34;; } protected void Transfer(object sender, EventArgs e) { decimal total = 0; try { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[9] { new DataColumn(&#34;Name&#34;), new DataColumn(&#34;Quantity&#34;), new DataColumn(&#34;Contact&#34;), new DataColumn(&#34;Email&#34;), new DataColumn(&#34;State&#34;), new DataColumn(&#34;District&#34;), new DataColumn(&#34;Address&#34;), new DataColumn(&#34;PQuantity&#34;), new DataColumn(&#34;Total&#34;) }); foreach (GridViewRow row in gvData.Rows) { if (row.RowType == DataControlRowType.DataRow) { CheckBox chkRow = (row.Cells[0].FindControl(&#34;chk&#34;) as CheckBox); if (chkRow.Checked) { string Name = (row.Cells[1].FindControl(&#34;Name&#34;) as Label).Text; string Quantity = (row.Cells[2].FindControl(&#34;Quantity&#34;) as Label).Text; string Contact = (row.Cells[3].FindControl(&#34;Contact&#34;) as Label).Text; string Email = (row.Cells[4].FindControl(&#34;Email&#34;) as Label).Text; string State = (row.Cells[5].FindControl(&#34;State&#34;) as Label).Text; string District = (row.Cells[6].FindControl(&#34;District&#34;) as Label).Text; string Address = (row.Cells[7].FindControl(&#34;Address&#34;) as Label).Text; string PQuantity = (row.Cells[8].FindControl(&#34;PQuantity&#34;) as Label).Text; string Total = (row.Cells[9].FindControl(&#34;Total&#34;) as Label).Text; total += Convert.ToDecimal(Total); dt.Rows.Add(Name, Quantity, Contact, Email, State, District, Address, PQuantity, Total); } } } gvSelected.DataSource = dt; gvSelected.DataBind(); } catch (Exception ex) { Response.Write(ex); } }</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/340224/Update-Checked-Selected-GridView-Rows-based-on-Id-using-C-in-ASPNet/https://www.aspforums.net:443/Threads/340224/Update-Checked-Selected-GridView-Rows-based-on-Id-using-C-in-ASPNet/Fri, 16 Aug 2019 02:05:38 GMT