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.Display colored Rectangle in ASP.Net GridView Column from Database using C# and VB.Net<p>Hi&nbsp;democloud,</p> <p>GridView doesn't provide the capability to draw some elements in its cells. As a possible solution, you can&nbsp;make use of Label control in the&nbsp;ItemTemplate field and add a font&nbsp;awesome icon&nbsp;as a Text for the Label control and from code behind set the&nbsp;<span style="text-decoration: underline;">ForeColor</span> property to display the color.</p> <p>Check this example. Now please take its reference and correct your code.</p> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">&lt;link rel=&#34;stylesheet&#34; href=&#34;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css&#34; /&gt; &lt;asp:GridView ID=&#34;GridView1&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;false&#34; OnRowDataBound=&#34;GridView1_RowDataBound&#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;asp:TemplateField HeaderText=&#34;Color&#34; ItemStyle-HorizontalAlign=&#34;Center&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:HiddenField ID=&#34;hfColor&#34; runat=&#34;server&#34; Value=&#39;&lt;%# Eval(&#34;Color&#34;) %&gt;&#39; /&gt; &lt;asp:Label ID=&#34;lblColor&#34; runat=&#34;server&#34;&gt;&lt;i class=&#34;fa fa-square fa-2x&#34;&gt;&lt;/i&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt;</pre> <p><strong><span style="text-decoration: underline;">Namespaces</span></strong></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><strong><span style="text-decoration: underline;">Code</span></strong></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.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)), new DataColumn("Name", typeof(string)), new DataColumn("Color",typeof(string)) }); dt.Rows.Add(1, "Chai", "#FF0000"); dt.Rows.Add(2, "Dal", "#FFFF00"); dt.Rows.Add(3, "Leaf", "#00FF00"); GridView1.DataSource = dt; GridView1.DataBind(); } } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string color = (e.Row.FindControl("hfColor") as HiddenField).Value; (e.Row.FindControl("lblColor") as Label).ForeColor = Color.FromArgb(System.Drawing.ColorTranslator.FromHtml(color).ToArgb()); } }</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.AddRange(New DataColumn(2) { New DataColumn("Id", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Color", GetType(String))}) dt.Rows.Add(1, "Chai", "#FF0000") dt.Rows.Add(2, "Dal", "#FFFF00") dt.Rows.Add(3, "Leaf", "#00FF00") GridView1.DataSource = dt 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 Dim color As String = (TryCast(e.Row.FindControl("hfColor"), HiddenField)).Value TryCast(e.Row.FindControl("lblColor"), Label).ForeColor = System.Drawing.Color.FromArgb(ColorTranslator.FromHtml(color).ToArgb()) End If End Sub</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><img src="https://i.imgur.com/G1UrgPR.jpg" alt="" width="251" height="238" /></p>https://www.aspforums.net:443/Threads/612415/Display-colored-Rectangle-in-ASPNet-GridView-Column-from-Database-using-C-and-VBNet/https://www.aspforums.net:443/Threads/612415/Display-colored-Rectangle-in-ASPNet-GridView-Column-from-Database-using-C-and-VBNet/Mon, 10 Dec 2018 03:56:09 GMT