2012-02-14 37 views
-5

我有以下的數據庫設計:如何在C#中爲每個HTMLTable標題指定一個特定的顏色?

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

課程表:CourseID,CourseName,羣ID

Employee_Courses表:僱員,CourseID

Group Table: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" /> 

注:羣ID將從SqlDataSource1被retrived,然後將使用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

重複http:// stackoverflow。com/questions/9266315/how-to-give-each-table-or-group-of-course-a-specific-color-in-this-htmltable and http://stackoverflow.com/questions/9271472/how- to-each-table-header-or-group-of-course-a-specific-color-in-this-htm – 2012-02-14 11:40:03

+0

這是怎麼回事?你複製相同的代碼?你在同一班?同一所學校?與健忘症相同的人:)) – Aristos 2012-02-14 11:59:48

回答

0

這樣做每次添加標題行此。 ..

header.Style.Add("background-color", "Red") 
table.Rows.Add(header); 
相關問題