2012-02-13 52 views
0

我有以下的數據庫設計:如何在此HTMLTable中爲每個表(或一組課程)提供特定的顏色?

Employee表:用戶名,姓名,工作...等

課程表:CourseID,CourseName,羣ID

Employee_Courses表:僱員,CourseID

組表:組ID,組名

注意:每個表中的第一個屬性是主鍵

我已經開發了一個矩陣,顯示所有員工和所有課程。由於我有三組三門課程,所以每組課程需要一張桌子。我開發了這個矩陣來查看使用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" /> 

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

     } 
    } 

回答

0

創建一個函數,將組名稱作爲參數並返回所需的顏色,然後從您的co de或標記顏色需要設置的地方。您可以返回一個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; 
} 
0

你不能這樣做嗎?

首先在您的樣式表中創建所需顏色的類。

.redcell{background:#ff0000;} 

然後在您的代碼中添加單元格。

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