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.Delete records from DataTable using Linq in ASP.Net<p>Hi <a class="username" rel="sanjay8090"> sanjay8090</a>,</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:GridView runat=&#34;server&#34; ID=&#34;GridView1&#34; AutoGenerateColumns=&#34;false&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;Id&#34; HeaderText=&#34;Id&#34; /&gt; &lt;asp:BoundField DataField=&#34;Name&#34; HeaderText=&#34;Name&#34; /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;asp:Button Text=&#34;Delete&#34; runat=&#34;server&#34; OnClick=&#34;Delete&#34; /&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) { DataTable dt = new DataTable(); dt.Columns.Add(&#34;Id&#34;); dt.Columns.Add(&#34;Name&#34;); dt.Rows.Add(1, &#34;Ajeet&#34;); dt.Rows.Add(2, &#34;Dharmendra&#34;); dt.Rows.Add(3, &#34;Ravi&#34;); dt.Rows.Add(4, &#34;Anirudha&#34;); GridView1.DataSource = dt; GridView1.DataBind(); ViewState[&#34;dt&#34;] = dt; } } protected void Delete(object sender, EventArgs e) { DataTable dt = ViewState[&#34;dt&#34;] as DataTable; var result = dt.AsEnumerable().Take(3); foreach (var row in result.ToList()) { row.Delete(); } GridView1.DataSource = dt; GridView1.DataBind(); }</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 Dim dt As DataTable = New DataTable() dt.Columns.Add(&#34;Id&#34;) dt.Columns.Add(&#34;Name&#34;) dt.Rows.Add(1, &#34;Ajeet&#34;) dt.Rows.Add(2, &#34;Dharmendra&#34;) dt.Rows.Add(3, &#34;Ravi&#34;) dt.Rows.Add(4, &#34;Anirudha&#34;) GridView1.DataSource = dt GridView1.DataBind() ViewState(&#34;dt&#34;) = dt End If End Sub Protected Sub Delete(ByVal sender As Object, ByVal e As EventArgs) Dim dt As DataTable = TryCast(ViewState(&#34;dt&#34;), DataTable) Dim result = dt.AsEnumerable().Take(3) For Each row In result.ToList() row.Delete() Next GridView1.DataSource = dt GridView1.DataBind() End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/BvdK8gn.gif" alt="" width="231" height="174" /></p>https://www.aspforums.net:443/Threads/375546/Delete-records-from-DataTable-using-Linq-in-ASPNet/https://www.aspforums.net:443/Threads/375546/Delete-records-from-DataTable-using-Linq-in-ASPNet/Mon, 14 Oct 2019 06:30:01 GMT