动态生成GridVier的列且得到行记录

来源:互联网 发布:淘宝抓图工具 编辑:程序博客网 时间:2024/05/21 06:26

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="IntegratedEvaluation.aspx.cs" Inherits="XG_Evaluate_IntegratedEvaluation" %>
<%@ Register Src="../../_Controls/NavigationBanner.ascx" TagName="NavigationBanner"
    TagPrefix="uc2" %>
<%@ Register Src="../../_Controls/Pagination.ascx" TagName="Pagination" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>学生期末德育测评</title>
    <link href="../../_CSS/Common.css" rel="stylesheet" type="text/css" />
    <script src="../../_Scripts/DateDropDown.js" type="text/javascript"></script>
    <script src="../../_Scripts/Common.js" type="text/javascript"></script>
   
</head>
<body onload="initImageButton()">
    <div id="messageArea"></div>
    <form id="form1" runat="server">
        <uc2:NavigationBanner ID="NavigationBanner1" runat="server" />
        <table style="width: 100%; border-bottom: #B0B0B0 6px solid; font-size: 9pt;" border="0" cellspacing="0" cellpadding="0">
           <tr style="height: 24px; background-color: #F3F3F1;">
             <td style="width: 18px; height: 24px;"><img src="../../_Images/Neutral/Page/titleIcon.jpg" width="16" height="20" alt="" /></td>
             <td style="background-image: url(../../_Images/Neutral/Page/bannerTop.jpg); background-repeat: repeat-x; height: 24px;">
               <span class="PageTitle" id="PageTitle">
      学生期末德育测评</span></td>
          </tr>
       </table>
       <table border="0" cellpadding="0" cellspacing="0" style="width: 100%; height: 20px;  background-color: #326383; font-size: 9pt;">
          <tr>
       <td  width="4%" height="20" align="left" valign="middle" rowspan="2" >
                 <img src="../../_Images/Neutral/Page/bannerBlueLeft.gif" alt="" /></td>
                
       <td align="right" valign="middle" style="width: 10%; height: 10px;">
                 <span style="color: #ffffff"><strong>学年</strong></span></td> 
       <td style="height: 10px; width: 172px;" align="left" valign="middle">
                 <asp:DropDownList ID="ddlYear" runat="server" Width="149px"></asp:DropDownList></td>
            
       <td style="width: 10%; height: 10px;"  align ="right">
                 <span style="color: #ffffff"><strong>学期</strong></span></td>
       <td style="width: 12%; height: 10px">      
        <asp:DropDownList ID="ddlSemester" runat="server" MaxLength="50" Width="168px">
                     <asp:ListItem Selected="True" Value="1">春季学期</asp:ListItem>
                     <asp:ListItem Value="2">秋季学期</asp:ListItem>
                 </asp:DropDownList></td>
        
       <td style="width: 10%; height: 10px;"  align ="right"><span style="color: #ffffff"><strong>班级</strong></span></td>
       <td width="10%" style="width: 10%; height: 10px">      
        <asp:DropDownList ID="ddlClass" runat="server" MaxLength="50" Width="143px"></asp:DropDownList></td>
             
       <td align="right" rowspan="2">
                  <asp:ImageButton ID="imgBtnSearch" runat="server" ToolTip="查询"  ImageUrl="~/_Images/zh-cn/Button/queryBlue.gif" meta:resourcekey="imgBtnSearchResource1" OnClick="imgBtnSearch_Click"
         /></td>
             <td width="4%" rowspan="2" align="right" style="width: 5%; height: 10px;" ><img src="../../_Images/Neutral/Page/bannerBlueRight.gif" alt="" /></td>
       </tr>
       </table>
      
      <table width="100%" border="0" cellspacing="0" cellpadding="0" style="font-size: 9pt">
         <tr>
            <td colspan="6" style="height: 30px">
                <asp:GridView ID = "gvUnderGraduateList" runat = "server" AutoGenerateColumns = "False" CellPadding = "4"
                   CssClass="TableGrey1px" Font-Size="9pt" ForeColor="#333333" GridLines="Vertical" Height="30px" Width="100%">
                       <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                       <RowStyle BackColor="#EFF3FB" />
                       <EditRowStyle BackColor="#2461BF" />
                       <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                       <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                       <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                       <AlternatingRowStyle BackColor="White" />
                </asp:GridView>
                <div id="div2" style="right: 1px; top: 17px"></div>
               
            </td>
          </tr>
        </table>
     
     
    </form>
</body>
</html>

 -----------------------

C#

 public class GridViewTemplate : ITemplate
    {
        private DataControlRowType templateType;
        private string columnName;
        private int tbWidth;

        public GridViewTemplate(DataControlRowType type, string colname,int pWidth)
        {
            templateType = type;
            columnName = colname;
            tbWidth = pWidth;
        }

        public void InstantiateIn(System.Web.UI.Control container)
        {
            switch (templateType)
            {
                case DataControlRowType.Header:
                    Literal lc = new Literal();
                    lc.ID = columnName;
                    container.Controls.Add(lc);
                    break;
                case DataControlRowType.DataRow:
                    TextBox tb = new TextBox();
                    tb.ID = columnName.ToString();
                    //tb.Text = tb.id.ToString();
                    tb.AutoPostBack = false;
                    tb.Width = tbWidth;
                    container.Controls.Add(tb);
                    break;
                default:
                    break;
            }
        }
    }
    //根据数据表测评项目的记录动太生成GridView的列
    private void CreateGridviewColumns()
    {
        string returnMessage = "";
        int rowCount = 0;
        DataSet dsProject = new DataSet();
        DataSet dsList = new DataSet();
        returnMessage = IEWebService.GetInfoWithAppointCondition(1, 100, ref rowCount, "dm_dypcxm", "2", "2", "dypcxm_dm", ref dsProject);
        returnMessage = IEWebService.GetUnderGraduateWithClassID("227F37F041D34F22E0440003BA449C4F", ref dsList);
        gvUnderGraduateList.DataSource = dsList;
        if (returnMessage == null)
        {
            BoundField bfxm = new BoundField();
            bfxm.HeaderText = "姓名";
            bfxm.DataField = "XSXM";
            gvUnderGraduateList.Columns.Add(bfxm);
            foreach (DataRow dr in dsProject.Tables[0].Rows)
            {
                TemplateField tf = new TemplateField();
                tf.ShowHeader = true;
                tf.HeaderText = dr[2].ToString();
               
                //bfxm.DataField = "XSXM";
                tf.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, dr[1].ToString(), (800 / dsProject.Tables[0].Rows.Count));
                gvUnderGraduateList.Columns.Add(tf);
            }
            returnMessage = "数据检索完毕。";
        }
        ShowMessageArea(returnMessage, 1);
        gvUnderGraduateList.DataBind();
    }

原创粉丝点击