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.Add Column in GridView on Run Time using LINQ in ASP.Net<p>Hi <a class="username" rel="nauna">nauna</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; onrowdatabound=&#34;GridView1_RowDataBound&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;CustomerId&#34; HeaderText=&#34;CustomerId&#34; /&gt; &lt;asp:BoundField DataField=&#34;ContactName&#34; HeaderText=&#34;ContactName&#34; /&gt; &lt;asp:BoundField DataField=&#34;Country&#34; HeaderText=&#34;Country&#34; /&gt; &lt;asp:TemplateField HeaderText=&#34;Rank&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&#34;lblRank&#34; runat=&#34;server&#34; /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt;</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) { TestEntities entity = new TestEntities(); var query = (from c in entity.TestCustomers select c).ToList().Take(4); List&lt;object&gt; result = new List&lt;object&gt;(); foreach (object item in query) { result.Add(item); } GridView1.DataSource = result; GridView1.DataBind(); } } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if (e.Row.Cells[0].Text == &#34;AAFKM&#34;) { (e.Row.FindControl(&#34;lblRank&#34;) as Label).Text = &#34;first&#34;; } else if (e.Row.Cells[0].Text == &#34;ALFKI&#34;) { (e.Row.FindControl(&#34;lblRank&#34;) as Label).Text = &#34;second&#34;; } else if (e.Row.Cells[0].Text == &#34;ANATR&#34;) { (e.Row.FindControl(&#34;lblRank&#34;) as Label).Text = &#34;third&#34;; } else if (e.Row.Cells[0].Text == &#34;ANTON&#34;) { (e.Row.FindControl(&#34;lblRank&#34;) as Label).Text = &#34;fourth&#34;; } } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) If Not Me.IsPostBack Then Dim entity As TestEntities = New TestEntities() Dim query = (From c In entity.TestCustomers Select c).ToList().Take(4) Dim result As List(Of Object) = New List(Of Object)() For Each item As Object In query result.Add(item) Next GridView1.DataSource = result GridView1.DataBind() End If End Sub Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) If e.Row.RowType = DataControlRowType.DataRow Then If e.Row.Cells(0).Text = &#34;AAFKM&#34; Then (TryCast(e.Row.FindControl(&#34;lblRank&#34;), Label)).Text = &#34;first&#34; ElseIf e.Row.Cells(0).Text = &#34;ALFKI&#34; Then (TryCast(e.Row.FindControl(&#34;lblRank&#34;), Label)).Text = &#34;second&#34; ElseIf e.Row.Cells(0).Text = &#34;ANATR&#34; Then (TryCast(e.Row.FindControl(&#34;lblRank&#34;), Label)).Text = &#34;third&#34; ElseIf e.Row.Cells(0).Text = &#34;ANTON&#34; Then (TryCast(e.Row.FindControl(&#34;lblRank&#34;), Label)).Text = &#34;fourth&#34; End If End If End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/MiQ62Gk.jpg" alt="" width="326" height="216" /></p>https://www.aspforums.net:443/Threads/114379/Add-Column-in-GridView-on-Run-Time-using-LINQ-in-ASPNet/https://www.aspforums.net:443/Threads/114379/Add-Column-in-GridView-on-Run-Time-using-LINQ-in-ASPNet/Mon, 09 Sep 2019 07:55:00 GMT