本文介绍了如何在此HTMLTable中为每个表格标题(或课程组)赋予特定的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下数据库设计:

>员工表:用户名,名称,工作...等
>
>课程表:CourseID,CoursName,GroupID
>
> Employee_Courses表:EmployeeID,CourseID
>
>群组表:GroupID,GroupName


***注意:每个表中的第一个属性是主键***

我已经建立了一个矩阵,该矩阵显示所有员工和所有课程.由于我有三门课程,每门课程需要三张桌子,因此我需要为每门课程提供一张桌子.我开发了此矩阵,以便在Repeater控件中使用GridView查看信息.另外,我再次开发了使用C#中的HTMLTable输入数据的方法.一切正常.我现在需要给每个组指定特定的颜色.例如,具有蓝色的Group#1和具有黄色的Group#2,依此类推.我现在正在用C#进行此操作.

**那么有人可以帮我解决这个问题吗?**

我在ASP.NET和C#中的代码如下:

ASP.NET:

I have the following database design:

> Employee Table: Username, name, Job... etc
>
> Course Table: CourseID, CourseName, GroupID
>
> Employee_Courses Table: EmployeeID, CourseID
>
> Group Table: GroupID, GroupName


***NOTE: The first attribute in each table is the primary key***

I have developed a matrix that shows all employees and all courses. Since I have three groups of courses three, I need to have one table for each group of courses. I developed this matrix to view the information using GridView inside a Repeater control. Also, I developed again for entering data using the HTMLTable in C#. Everything works fine. What I need now is to give each group specific color. For example, Group#1 with Blue color and Group#2 with Yellow color and so on. I am struggling now with doing this in C#.

**So could anyone please help me with this issue?**

My code in ASP.NET and C# is as following:

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中检索,然后将在SqlDataSource2中使用

C#:




**NOTE:** GroupID will be retrived from SqlDataSource1, and then will be used in 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);

        }
    }

推荐答案






**注意:**组ID将从SqlDataSource1中检索,然后将在SqlDataSource2中使用

C#:




**NOTE:** GroupID will be retrived from SqlDataSource1, and then will be used in 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);

        }
    }


这篇关于如何在此HTMLTable中为每个表格标题(或课程组)赋予特定的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 09:09