【问题标题】:How to give each HTMLTable header a specific color in C#?如何在 C# 中为每个 HTMLTable 标题指定特定颜色?
【发布时间】:2012-02-14 11:34:19
【问题描述】:

我有以下数据库设计:

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

课程表:CourseID、CourseName、GroupID

Employee_Courses 表:EmployeeID、CourseID

组表:GroupID、GroupName

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

我开发了一个矩阵来显示所有员工和所有课程。由于我有三组课程三,我需要为每组课程准备一张桌子。我开发了这个矩阵来使用Repeater 控件中的GridView 查看信息。此外,我再次开发了使用 C# 中的 HTMLTable 输入数据。一切正常。我现在需要的是给每个组特定的颜色。例如,Group#1 为蓝色,Group#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>

【问题讨论】:

标签: c# asp.net


【解决方案1】:

每次添加标题行时都执行类似操作...

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    • 2021-09-21
    • 2013-05-22
    相关资源
    最近更新 更多