我爲我公司開發了一個培訓矩陣,向所有員工展示了公司提供的所有培訓課程。現在,我必須以易於管理員編輯和更新的方式進行開發。我做了一切正確的事情,除了從第四個單元開始到最後一個單元爲每組課程提供特定顏色(因爲我有3種類型的課程)。僅供參考,我有兩個SqlDataSources發展此矩陣:如何爲HTML表格中的某些特定單元格着色?
SqlDataSource1是提取羣ID:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT [ID] FROM [groups]"></asp:SqlDataSource>
而SqlDataSource2將從SqlDataSource1採取羣ID,並用它來生成矩陣:
<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>
現在,因爲我使用HTMLTABLE,我需要訪問SqlDataSource1做一些邏輯,如: 如果GroupID = 1,那麼給這個組分配藍色等等。我能夠做這樣做如下:
我的代碼隱藏在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"] = "uGrid";
//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;
//The following if-else statements are for checking the GroupID and give each group
//a specific color
if (group[0].ToString().Equals("1"))
headerCell.BgColor = "lightBlue";
else if (group[0].ToString().Equals("2"))
headerCell.BgColor = "lightYellow";
else if (group[0].ToString().Equals("3"))
headerCell.BgColor = "Orange";
//the header cells to the header
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;
//The following if-else statements are for checking the GroupID and give each group
//a specific color
if (group[0].ToString().Equals("1"))
headerCell.BgColor = "lightBlue";
else if (group[0].ToString().Equals("2"))
headerCell.BgColor = "lightYellow";
else if (group[0].ToString().Equals("3"))
headerCell.BgColor = "Orange";
//the header cells to the header
header.Cells.Add(headerCell);
}
table.Rows.Add(header);