我有以下数据库设计:
员工表:用户名,姓名,工作......等
课程表:CourseID,CourseName,GroupID
Employee_Courses表:EmployeeID,CourseID
组表: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" />
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 :(得分:0)
创建一个函数,该函数将组名作为参数并返回所需的颜色,然后从需要设置颜色的代码或标记中调用该函数。您可以返回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;
}
答案 1 :(得分:0)
首先在样式表中为您想要的颜色创建一个类。
.redcell{background:#ff0000;}
然后在您添加单元格的代码中。
if(Group=="mygroup")
{
cell.Attributes.Add("class","redcell");
}