如何在C#中为每个HTMLTable标题赋予特定颜色?

时间:2012-02-14 11:34:19

标签: c# asp.net

我有以下数据库设计:

员工表:用户名,姓名,工作......等

课程表:CourseID,CourseName,GroupID

Employee_Courses表:EmployeeID,CourseID

组表:GroupID,GroupName

注意:每个表中的第一个属性是主键

我开发了一个显示所有员工和所有课程的矩阵。由于我有三组课程三,我需要为每组课程设一个表。我开发了这个矩阵,在Repeater控件中使用GridView查看信息。此外,我再次开发使用C#中的HTMLTable输入数据。一切正常。我现在需要的是给每组特定的颜色。例如,组#1为蓝色,组#2为黄色,依此类推。我现在正在C#中做这件事。

所以有人可以帮我解决这个问题吗?

我在ASP.NET和C#中的代码如下:

ASP.NET:

<asp:PlaceHolder ID="PlaceHolder1" runat="server" />

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


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

                            <SelectParameters>
                                <asp:Parameter  Name="GroupID"/>
                            </SelectParameters>

                            <FilterParameters>
                                <asp:ControlParameter ControlID="ddlDivision" Name="DivisionName"
                                                         PropertyName="SelectedValue" Type="String" />
                            </FilterParameters>

            </asp:SqlDataSource>

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

            <asp:Button ID="updateButton" runat="server" OnClick="updateButton_Click" Text="Update" />

注意: GroupID将从SqlDataSource1中重新获得,然后将在SqlDataSource2中使用

C#:

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 < columns; j++)
                {
                    cell = new HtmlTableCell();
                    if (j < 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);

        }
    }

编辑:

我尝试通过这样做来解决它:

//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);

但我失败了,我收到以下错误: 无法将类型为“System.Data.DataView”的对象强制转换为“System.Data.SqlClient.SqlDataReader”

我不知道为什么。有什么帮助吗?

注意: 的 仅供参考,SqlDataSource1适用于:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:testConnectionString %>" 
                SelectCommand="SELECT [ID] FROM [groups]"></asp:SqlDataSource>

SqlDataSource2用于:

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

                            <SelectParameters>
                                <asp:Parameter  Name="GroupID"/>
                            </SelectParameters>

                            <FilterParameters>
                                <asp:ControlParameter ControlID="ddlDivision" Name="DivisionName" 
                                                         PropertyName="SelectedValue" Type="String" />
                            </FilterParameters>

            </asp:SqlDataSource>

1 个答案:

答案 0 :(得分:0)

每次添加标题行时都要这样做......

header.Style.Add("background-color", "Red")
table.Rows.Add(header);