我为公司开发了一个培训矩阵,向所有员工展示了公司提供的所有培训课程。现在,我必须以一种易于管理员编辑和更新的方式进行开发。我做了所有正确的事情,除了一件事情是给每组课程都指定了特定的颜色(因为我有3种类型的课程)。仅供参考,我有两个SqlDataSources用于开发此矩阵:
SqlDataSource1用于检索GroupID:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT [ID] FROM [groups]"></asp:SqlDataSource>
SqlDataSource2将从SqlDataSource1中获取GroupID并使用它来生成矩阵:
<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;
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);
编辑:
顺便说一句,是否可以确定哪些单元格将用蓝色着色?例如,在具有8门课程的第1组中,我希望着色从第四个单元格开始直到最后一个单元格。鉴于第2组有10门课程,我也希望着色从第四个单元开始,直到最后一个单元。您能帮我解决这个问题吗?
最佳答案
请告诉您,您是否确定DataView中的第一个DataRowViews项与查询中的GroupId相对应?并尝试将ToString()
方法应用于从最后一个代码段的DataRowView的第一项中检索的值,如下所示:
if (group[0].ToString().Equals("1"))
headerCell.BgColor = "Blue";
另外,要更改表中行的颜色-您需要更改数据字段的颜色-甚至最好更改整个行的颜色,而不是更改标题的颜色。
编辑:
要更改表的数据字段背景色,可以在填充表时分配BgColor实例的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);
// set field background color
if (group[0].Equals(1)){
cell.BgColor = "Blue";
else if (group[0].Equals(2))
cell.BgColor = "Yellow";
}
编辑2:
要设置标题单元格的颜色,我建议使用等式运算符替换Equals方法的使用(从具有特定索引的列开始):
//for adding the headers to the table
int counter = 0;
foreach (DataColumn column in dv.Table.Columns)
{
HtmlTableCell headerCell = new HtmlTableCell("th");
headerCell.InnerText = column.Caption;
if (++counter >= 5)
{
if (int.Parse(group[0]) == 1){
headerCell.BgColor = "Blue";
else if (int.Parse(group[0]) == 2)
headerCell.BgColor = "Yellow";
header.Cells.Add(headerCell);
}
}
table.Rows.Add(header);
关于c# - 如何访问以下SqlDataSource?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9288083/