title: How to dynamically color cells in a GridView if amount of columns is unknown?
body: <p>I have a dynamically generated GridView with unknown amount of columns. The data in the cells is 1 and 0 (Matrix type values). Is there a way to color cells with 1s in Green and 0s in White? </p>
  
  <p>CODE in ASP.NET:  </p>
  
  <pre><code>  &lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="Search.aspx.cs" Inherits="Search" %&gt;
  
  &lt;!DOCTYPE html&gt;    
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;&lt;/title&gt;    
      &lt;link href="StyleSheet.css" rel="stylesheet" type="text/css" /&gt; 
      &lt;style type="text/css"&gt;
          .auto-style1 {
              height: 223px;
              text-align: center;
              margin-top: 0px;
          }
          .auto-style2 {
              text-align: left;
          }
          .auto-style3 {
              float: left;
          }
          .auto-style4 {
              margin-top: 0px;
          }
          .auto-style5 {
              height: 8px;
              margin-top: 0px;
          }
          .auto-style6 {
              height: 8px;
          }
          .auto-style7 {
              font-size: medium;
          }
          .auto-style8 {
              float: left;
              margin-left: 9px;
          }
          .auto-style10 {
              float: left;
              width: 768px;
              height: 61px;
              margin-left: 9px;
              margin-top: 9px;
          }
      &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
          &lt;div class="auto-style1"&gt;
      &lt;div class="auto-style28"&gt;    
  
          &lt;img class="auto-style10" src="Images/Channel%20Map.png" /&gt;&lt;br /&gt;
          &lt;br /&gt;
          &lt;br /&gt;
          &lt;br /&gt;
      &lt;hr width="Auto" class="auto-style5" /&gt;
  
          &lt;table class="auto-style4"&gt;
              &lt;tr&gt;
                  &lt;td class="auto-style2"&gt;&lt;span style="font-family: Arial"&gt;
                      &amp;nbsp;&lt;span class="auto-style7"&gt;&amp;nbsp; FILTER&lt;/span&gt;:&lt;br /&gt;
                      &lt;br /&gt;
                      &lt;asp:DropDownList ID="ddl1" runat="server" AutoPostBack="true" CssClass="auto-style8" Height="25px"  Width="240px" OnSelectedIndexChanged="ddl1_SelectedIndexChanged" style="font-size: medium"&gt;
                          &lt;asp:ListItem Text="Select Item" Value="-1"&gt;&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem Text="Region     " Value="0"&gt;&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem Text="HQ         " Value="1"&gt;&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem Text="DMA        " Value="2"&gt;&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem Text="Company    " Value="3"&gt;&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem Text="Machine    " Value="4"&gt;&lt;/asp:ListItem&gt;
                          &lt;asp:ListItem Text="Zone       " Value="5"&gt;&lt;/asp:ListItem&gt;
                      &lt;/asp:DropDownList&gt;
                      &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
                      &lt;asp:DropDownList ID="ddl2" runat="server" CssClass="auto-style15" Height="25px" Margin-Left="-12px" Width="250px" AutoPostBack="False" style="font-size: medium" &gt;
                      &lt;/asp:DropDownList&gt;
                      &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
                      &lt;/span&gt; 
                      &lt;asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Run Report" Width="104px" CssClass="auto-style25" /&gt;
                      &amp;nbsp;&amp;nbsp;&amp;nbsp;
                      &lt;br /&gt;
                      &lt;br /&gt;
                      &lt;span class="auto-style3"&gt;&lt;span class="auto-style7" style="color: rgb(0, 0, 0); font-size: medium; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px;"&gt;&lt;span&gt;&lt;span class="auto-style23"&gt;&amp;nbsp;&amp;nbsp; &lt;asp:Label ID="Label2" runat="server" Text="CHANNELS FOUND: "&gt;&lt;/asp:Label&gt;
                      &amp;nbsp;&lt;asp:Label ID="Label1" runat="server" Text=""&gt;&lt;/asp:Label&gt;
                      &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp; &lt;/td&gt;
              &lt;/tr&gt;
          &lt;/table&gt;
  
      &lt;/div&gt;
      &lt;hr width="Auto" class="auto-style6" /&gt;
  
          &lt;asp:GridView id="GridView1" runat="server"  DataSourceID="SqlDataSource1"  OnRowDataBound="GridView1_RowDataBound" OnDataBound="gv_DataBound"   Width="274px" EnableModelValidation="True" HorizontalAlign="Left" CssClass="rwd-table"&gt;
              &lt;AlternatingRowStyle Wrap="False" /&gt;
              &lt;EditRowStyle Wrap="False" /&gt; 
  
              &lt;HeaderStyle Width="75px" BorderStyle="Inset" BorderWidth="3px" Wrap="False" /&gt;
              &lt;RowStyle Wrap="False" /&gt;
              &lt;SelectedRowStyle Wrap="False" /&gt;
          &lt;/asp:GridView&gt;
  
      &lt;asp:SqlDataSource id="SqlDataSource1" runat="server" 
       ConnectionString="&lt;%$ ConnectionStrings:ConString %&gt;" 
       SelectCommand="Usp_GetWHEREfilter" SelectCommandType="StoredProcedure"&gt;
          &lt;SelectParameters&gt;
              &lt;asp:FormParameter DefaultValue="" FormField="ddl2" Name="Filter" Type="String" /&gt;
  
          &lt;/SelectParameters&gt;
      &lt;/asp:SqlDataSource&gt;
  
          &lt;/div&gt;
  
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>HERE IS AN EXAMPLE OF MY table:</p>
  
  <p>Cells with 1ns - should be green
  Cells with 0s  - should be white</p>
  
  <pre><code>______________________________________
  COLUMNS  n1 n2 n3 n4 n5 n6 ... n
  VALUE1   1  0  1  0  0  1  ... 0 
  VALUE2   0  0  1  0  1  1  ... 1
  VALUE3   0  1  1  0  0  1  ... 1
  VALUE4   1  1  1  0  0  1  ... 0
  .....    .  .  .  .  .  .  ... .
  VALUEn   1  0  1  1  0  1  ... 0
  ______________________________________
  </code></pre>
  
  <p>Thanks for you time and help!!!        </p>