我为我公司开发了一个培训矩阵,向所有员工展示了公司提供的所有培训课程。现在,我必须以易于管理员编辑和更新的方式进行开发。我做了一切正确的事,除了一件事是给每组课程一个特定的颜色(因为我有3种类型的课程)。仅供参考,我有两个SqlDataSources发展此矩阵:如何访问下面的SqlDataSource?
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做一些逻辑,如: 我f 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;
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;
if (group[0].Equals(1)){
headerCell.BgColor = "Blue";
else if (group[0].Equals(2))
headerCell.BgColor = "Yellow";
header.Cells.Add(headerCell);
}
table.Rows.Add(header);
编辑:
顺便说一句,是否可以确定哪些单元格将用蓝色着色?例如,在组#1,其具有8个疗程,我想着色从第四单元开始直到最后一个。然而,组2有10门课程,我也希望着色从第四个单元开始到最后一个。 你能帮我解决这个问题吗?
非常感谢。我非常感谢你的帮助。顺便说一下,是否可以确定哪些单元格将用蓝色着色?例如,在有8门课程的小组#1中,我想让着色从第四个单元开始到最后一个。然而,组2有10门课程,我也希望着色从第四个单元开始到最后一个。你能帮我解决这个问题吗? – 2012-02-15 07:12:46
@AliAhmed可以使用[BgColor](http://msdn.microsoft.com/en-US/library/system.web.ui.htmlcontrols.htmltablecell.bgcolor%28v=vs)更改表格字段的颜色。 80%29.aspx)HtmlTableCell类的属性。您可以在您通过DataView循环的代码片段中的“if”运算符的“else”块中分配它。在你的thrird代码片段附近添加单元格附近的注释'/ /创建HtmlTable的地方。 – 2012-02-15 07:55:26
谢谢,但我说的是标题单元格而不是行单元格。你能否给我提供代码片段? – 2012-02-15 07:59:55