【问题标题】:How to give each table (or group of courses) a specific color in this HTMLTable?如何在此 HTMLTable 中为每个表(或课程组)赋予特定颜色?
【发布时间】:2012-02-14 18:55:08
【问题描述】:

我有以下数据库设计:

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

课程表: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" />

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

        }
    }

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    创建一个以组名作为参数并返回所需颜色的函数,然后从需要设置颜色的代码或标记中调用该函数。您可以返回 CSS 类名的字符串或任何其他所需的显示方法。

    private string GroupColor(string groupName)
    {
         string returnColor = string.empty;
         switch(groupName.toUpper())
         {
              case "GROUP1":
                  returnColor = "green";
                  break;
               case "GROUP2":
                  returnColor = "blue";
                  break;
                //  More case statements as needed...
                default:
                  returnColor = "orange";
                  break;
          }
    
          return returnColor;
    }
    

    【讨论】:

      【解决方案2】:

      你不能这样做吗?

      首先在您的样式表中为您想要的颜色创建一个类。

      .redcell{background:#ff0000;}
      

      然后在您要添加单元格的代码中。

      if(Group=="mygroup")
      {
        cell.Attributes.Add("class","redcell");
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-15
        • 2015-05-18
        • 1970-01-01
        • 2018-12-09
        • 1970-01-01
        • 2020-07-19
        • 2013-10-04
        • 1970-01-01
        相关资源
        最近更新 更多