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.Change dynamically generated ASP.Net GridView cell Text Color using C# and VB.Net<p>Hey <a class="username" rel="SamMyat"> SamMyat</a>,</p> <p>Please refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTNL</strong></span></p> <pre class="brush: html">&lt;div&gt; &lt;asp:Panel ID=&#34;PanelResult&#34; runat=&#34;server&#34;&gt; &lt;/asp:Panel&gt; &lt;/div&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; using System.Drawing;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data Imports System.Drawing </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) { Bind_GridView(); } } private void Bind_GridView() { DataSet ds = new DataSet(); int j = 0; DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { new DataColumn(&#34;Consumer Name&#34;, typeof(string)), new DataColumn(&#34;Buying Count No&#34;, typeof(int)) }); dt.Rows.Add(&#34;James&#34;, 12); dt.Rows.Add(&#34;Marry&#34;, -13); ds.Tables.Add(dt); dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { new DataColumn(&#34;Consumer Name&#34;, typeof(string)), new DataColumn(&#34;Buying Count No&#34;, typeof(int)) }); dt.Rows.Add(&#34;James&#34;, -12); dt.Rows.Add(&#34;Marry&#34;, 13); ds.Tables.Add(dt); if (ds.Tables.Count &gt; 0) { for (int i = 0; i &lt; ds.Tables.Count; i++) { j = i + 1; GridView objGV = new GridView(); objGV.ID = &#34;ResultDG&#34; + j; objGV.AutoGenerateColumns = true; objGV.RowDataBound += new GridViewRowEventHandler(objGV_RowDataBound); objGV.DataSource = ds.Tables[i]; objGV.DataBind(); PanelResult.Controls.Add(objGV); } } } void objGV_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { int id = Convert.ToInt32(e.Row.Cells[1].Text); if (id &gt; 0) { e.Row.Cells[1].ForeColor = Color.Green; } else { e.Row.Cells[1].Text = Math.Abs(id).ToString(); e.Row.Cells[1].ForeColor = Color.Red; } } }</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 Bind_GridView() End If End Sub Private Sub Bind_GridView() Dim ds As DataSet = New DataSet() Dim j As Integer = 0 Dim dt As DataTable = New DataTable() dt.Columns.AddRange(New DataColumn() {New DataColumn(&#34;Consumer Name&#34;, GetType(String)), New DataColumn(&#34;Buying Count No&#34;, GetType(Integer))}) dt.Rows.Add(&#34;James&#34;, 12) dt.Rows.Add(&#34;Marry&#34;, -13) ds.Tables.Add(dt) dt = New DataTable() dt.Columns.AddRange(New DataColumn() {New DataColumn(&#34;Consumer Name&#34;, GetType(String)), New DataColumn(&#34;Buying Count No&#34;, GetType(Integer))}) dt.Rows.Add(&#34;James&#34;, -12) dt.Rows.Add(&#34;Marry&#34;, 13) ds.Tables.Add(dt) If ds.Tables.Count &gt; 0 Then For i As Integer = 0 To ds.Tables.Count - 1 j = i + 1 Dim objGV As GridView = New GridView() objGV.ID = &#34;ResultDG&#34; &amp; j objGV.AutoGenerateColumns = True AddHandler objGV.RowDataBound, AddressOf objGV_RowDataBound objGV.DataSource = ds.Tables(i) objGV.DataBind() PanelResult.Controls.Add(objGV) Next End If End Sub Private Sub objGV_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) If e.Row.RowType = DataControlRowType.DataRow Then Dim id As Integer = Convert.ToInt32(e.Row.Cells(1).Text) If id &gt; 0 Then e.Row.Cells(1).ForeColor = Color.Green Else e.Row.Cells(1).Text = Math.Abs(id).ToString() e.Row.Cells(1).ForeColor = Color.Red End If End If End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://imgur.com/W80ZLhj.gif" alt="" width="276" height="316" /></p>https://www.aspforums.net:443/Threads/144347/Change-dynamically-generated-ASPNet-GridView-cell-Text-Color-using-C-and-VBNet/https://www.aspforums.net:443/Threads/144347/Change-dynamically-generated-ASPNet-GridView-cell-Text-Color-using-C-and-VBNet/Thu, 03 Jan 2019 01:59:25 GMT