How to give each HTMLTable header a specific color in C#?
<p>I have the following database design:</p>

<p>Employee Table: Username, name, Job... etc</p>

<p>Course Table: CourseID, CourseName, GroupID</p>

<p>Employee_Courses Table: EmployeeID, CourseID</p>

<p>Group Table: GroupID, GroupName</p>

<p><em><strong>NOTE: The first attribute in each table is the primary key</em></strong></p>

<p>I have developed a matrix that shows all employees and all courses. Since I have three groups of courses three, I need to have one table for each group of courses. I developed this matrix to view the information using GridView inside a Repeater control. Also, I developed again for entering data using the HTMLTable in C#. Everything works fine. What I need now is to give each group specific color. For example, Group#1 with Blue color and Group#2 with Yellow color and so on. I am struggling now with doing this in C#.</p>

<p><strong>So could anyone please help me with this issue?</strong></p>

<p>My code in ASP.NET and C# is as following:</p>

<p><strong>ASP.NET:</strong></p>

<pre><code>&lt;asp:PlaceHolder ID="PlaceHolder1" runat="server" /&gt;

            &lt;%--This SqlDataSource is for retrieving the GroupID--%&gt;
            &lt;asp:SqlDataSource ID="SqlDataSource1" runat="server"
                ConnectionString="&lt;%$ ConnectionStrings:testConnectionString %&gt;"
                SelectCommand="SELECT [ID] FROM [groups]"&gt;&lt;/asp:SqlDataSource&gt;


            &lt;%--This SqlDataSource is for retrieving the information of the employees and the safety training coruses--%&gt;
            &lt;asp:SqlDataSource ID="SqlDataSource2" runat="server"
                                            ConnectionString="&lt;%$ ConnectionStrings:testConnectionString %&gt;"
                                            SelectCommandType="StoredProcedure" SelectCommand="kbiReport" FilterExpression="[DivisionName] like '{0}%'"&gt;

                            &lt;SelectParameters&gt;
                                &lt;asp:Parameter  Name="GroupID"/&gt;
                            &lt;/SelectParameters&gt;

                            &lt;FilterParameters&gt;
                                &lt;asp:ControlParameter ControlID="ddlDivision" Name="DivisionName"
                                                         PropertyName="SelectedValue" Type="String" /&gt;
                            &lt;/FilterParameters&gt;

            &lt;/asp:SqlDataSource&gt;

            &lt;%--Filtering by Division--%&gt;
            &lt;asp:SqlDataSource ID="sqlDataSourceDivision" runat="server"
            ConnectionString="&lt;%$ ConnectionStrings:testConnectionString %&gt;"
            SelectCommand="SELECT [DivisionName] FROM [Divisions]"&gt;&lt;/asp:SqlDataSource&gt;

            &lt;asp:Button ID="updateButton" runat="server" OnClick="updateButton_Click" Text="Update" /&gt;
</code></pre>

<p><strong>NOTE:</strong> GroupID will be retrived from SqlDataSource1, and then will be used in SqlDataSource2</p>

<p><strong>C#:</strong></p>

<pre><code>protected void Page_Load(object sender, EventArgs e)
    {

        DataView dv2 = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
        foreach (DataRowView group in dv2)
        {
            SqlDataSource2.SelectParameters[0].DefaultValue = group[0].ToString();
            //create a new HtmlTable object
            HtmlTable table = new HtmlTable();

            DataView dv = (DataView)SqlDataSource2.Select(DataSourceSelectArguments.Empty);
            int columns = dv.Table.Columns.Count;
            int rows = dv.Count;

            //table's formating-related properties
            table.Border = 2;
            table.CellPadding = 3;
            table.CellSpacing = 3;
            table.Width = "900px";

            //to get the css style
            table.Attributes["class"] = "mGrid";

            //create a new HtmlTableRow and HtmlTableCell objects
            HtmlTableRow row;
            HtmlTableRow header = new HtmlTableRow();
            HtmlTableCell cell;


            //for adding the headers to the table
            foreach (DataColumn column in dv.Table.Columns)
            {
                HtmlTableCell headerCell = new HtmlTableCell("th");
                headerCell.InnerText = column.Caption;
                //I need to specify the color for each group here
                header.Cells.Add(headerCell);
            }
            table.Rows.Add(header);

            //loop for adding rows to the table
            foreach (DataRowView datarow in dv)
            {
                row = new HtmlTableRow();
                row.BgColor = "yellow";


                //loop for adding cells
                for (int j = 0; j &lt; columns; j++)
                {
                    cell = new HtmlTableCell();
                    if (j &lt; 4)
                    {
                        cell.InnerText = datarow[j].ToString();
                    }
                    else
                    {

                        CheckBox checkbox = new CheckBox();

                        int checkBoxColumns = dv.Table.Columns.Count - 5;
                        string fieldvalue = datarow[j].ToString();
                        string yes = fieldvalue.Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries)[1];
                        string courseid = fieldvalue.Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries)[0];
                        checkbox.ID = row.Cells[3].InnerText + "," + courseid.Trim();
                        checkbox.Checked = yes.Equals("Yes");
                        cell.Controls.Add(checkbox);

                    }

                    //add the cell to the current row
                    row.Cells.Add(cell);
                }

                //add the row to the table
                table.Rows.Add(row);
            }

            //add the table to the page
            PlaceHolder1.Controls.Add(table);

        }
    }
</code></pre>

<p><strong>EDIT:</strong> </p>

<p>I tried to solve it by doing this:</p>

<pre><code>//for adding the headers to the table
            foreach (DataColumn column in dv.Table.Columns)
            {
                HtmlTableCell headerCell = new HtmlTableCell("th");
                headerCell.InnerText = column.Caption;

                //SqlDataSource DataReader Mode
                using (SqlDataReader rdrSql = (SqlDataReader)SqlDataSource1.Select(DataSourceSelectArguments.Empty))
            {
                while (rdrSql.Read())
                    if (rdrSql["ID"].Equals(1))
                        headerCell.BgColor = "lightBlue";
                    else if (rdrSql["ID"].Equals(2))
                        headerCell.BgColor = "lightYellow";
                    else if (rdrSql["ID"].Equals(3))
                        headerCell.BgColor = "lightOrange";
            }
                header.Cells.Add(headerCell);
            }
            table.Rows.Add(header);
</code></pre>

<p>But I failed and I got the following error:
Unable to cast object of type 'System.Data.DataView' to type 'System.Data.SqlClient.SqlDataReader'</p>

<p>and I don't know why. Any help please?</p>

<p><em><strong>NOTE:</em></strong>
FYI, the SqlDataSource1 is for:</p>

<pre><code>&lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="&lt;%$ ConnectionStrings:testConnectionString %&gt;" 
                SelectCommand="SELECT [ID] FROM [groups]"&gt;&lt;/asp:SqlDataSource&gt;
</code></pre>

<p>And the SqlDataSource2 is for:</p>

<pre><code>&lt;asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                                            ConnectionString="&lt;%$ ConnectionStrings:testConnectionString %&gt;" 
                                            SelectCommandType="StoredProcedure" SelectCommand="kbiReport" FilterExpression="[Division] like '{0}%'"&gt;

                            &lt;SelectParameters&gt;
                                &lt;asp:Parameter  Name="GroupID"/&gt;
                            &lt;/SelectParameters&gt;

                            &lt;FilterParameters&gt;
                                &lt;asp:ControlParameter ControlID="ddlDivision" Name="DivisionName" 
                                                         PropertyName="SelectedValue" Type="String" /&gt;
                            &lt;/FilterParameters&gt;

            &lt;/asp:SqlDataSource&gt;
</code></pre>
