CSDN博客

img yuweiqiang

Asp.net2.0 DataGrid多行表头与可编辑template模板的网格实现。

发表于2008/5/30 14:48:00  974人阅读

分类: DataGrid C# Asp.Net

 Aspx页面设计如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Attonperrpt.aspx.cs" Inherits="AppWebprogram_Attendance_Attrpt_Attonperrpt" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>考勤统计</title>
    <LINK href="../../../css/default.css" _fcksavedurl=""../../../css/default.css"" type="text/css" rel="stylesheet">
    <script language=javascript>
        function vUnload()
        {
            document.all["btnExit"].click();
            window.close();
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <table width="780" border="0" cellpadding="0" cellspacing="0" class="pagenavtop">
      <tr>
        <td align=left>所在位置:><span id="Current">考勤->在编员工考勤统计</span></td>
      </tr>
    </table>   
    <div id=basesize>
        <table width="780" border="0" cellpadding="0" cellspacing="0" class="formgrid">
            <tr>
            <td align=left>
                <a href="#" id="textbutton"><img src="../../../images/31.gif" width="19" height="15">在编员工</a>
                <asp:Label ID="lbUsrName" runat="server" Text="Admin" BorderStyle="Solid" BackColor="SlateBlue" Font-Bold="True" ForeColor="ActiveCaptionText" Width="116px"></asp:Label>
            </td>
            </tr>
        </table>     
        <table align=center width="780" border=1>
            <TR>
                <TD style="height: 24px;" >
                    区域:<asp:DropDownList ID="ddlArea" runat="server" AutoPostBack="True" ></asp:DropDownList>               
                </td>    
                <TD style="height: 24px" >
                    <asp:DropDownList ID="ddlYear" runat="server"></asp:DropDownList>年
                    <asp:DropDownList ID="ddlMonth" runat="server"></asp:DropDownList>月           
                </td>  
                <td>
                    <asp:Button ID="btnQuery" runat="server" CssClass="button"  Text="查询" OnClick="btnQuery_Click"/> 
                    <asp:Button ID="btnPrint" runat="server" CssClass="button"  Text="打印"/> 
                </td>             
            </TR>             
        </table>
   <TABLE id="Table1" cellSpacing="0" cellPadding="0" width="780" align="center" border="0">
    <TR>
     <TD>
     <asp:datagrid id="grDatalist" runat="server"  Width="100%" CssClass="DataGrid"
       ShowFooter="True" AllowPaging="True" BorderColor="Blue" AutoGenerateColumns="false">
       <ItemStyle CssClass="DataGridItem"></ItemStyle>
       <HeaderStyle CssClass="DataGridHeader" BackColor="#99CCCC" Font-Bold="True" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False"></HeaderStyle>
                        <PagerStyle Mode="NumericPages" />
                        <Columns>
                            <asp:BoundColumn DataField="id" HeaderText="序号"></asp:BoundColumn>
                            <asp:BoundColumn DataField="Person_id" HeaderText="姓名"></asp:BoundColumn>
                            <asp:TemplateColumn HeaderText="全勤">
                                <ItemTemplate>                               
                                    <asp:TextBox ID="txt1" Width=30px CssClass="text"  Text='<%# DataBinder.Eval(Container.DataItem, "B_all") %>' runat="server"/>                      
                                </ItemTemplate>                       
                                <HeaderStyle HorizontalAlign=center />
                                <ItemStyle HorizontalAlign=center />
                            </asp:TemplateColumn>     
                            <asp:TemplateColumn HeaderText="天数">
                                <ItemTemplate>                               
                                    <asp:TextBox ID="txt2" Width=30px CssClass="text" Text='<%# DataBinder.Eval(Container.DataItem, "B_xjDays") %>' runat="server"/>                      
                                </ItemTemplate>                       
                                <HeaderStyle />
                            </asp:TemplateColumn>   
                            <asp:TemplateColumn HeaderText="小时">
                                <ItemTemplate>                               
                                    <asp:TextBox ID="txt3" Width=30px CssClass="text" Text='<%# DataBinder.Eval(Container.DataItem, "B_xjhours") %>' runat="server"/>                      
                                </ItemTemplate>                       
                                <HeaderStyle />
                            </asp:TemplateColumn>  
                            <asp:TemplateColumn HeaderText="天数">
                                <ItemTemplate>                               
                                    <asp:TextBox ID="txt4" Width=30px CssClass="text" Text='<%# DataBinder.Eval(Container.DataItem, "B_qtDays") %>' runat="server"/>                      
                                </ItemTemplate>                       
                                <HeaderStyle />
                            </asp:TemplateColumn>   
                            <asp:TemplateColumn HeaderText="小时">
                                <ItemTemplate>                               
                                    <asp:TextBox ID="txt5" Width=30px CssClass="text" Text='<%# DataBinder.Eval(Container.DataItem, "B_qthours") %>' runat="server"/>                      
                                </ItemTemplate>                       
                                <HeaderStyle />
                            </asp:TemplateColumn>    
                            <asp:TemplateColumn HeaderText="迟到">
                                <ItemTemplate>                               
                                    <asp:TextBox ID="txt6" Width=30px CssClass="text" Text='<%# DataBinder.Eval(Container.DataItem, "C_late") %>' runat="server"/>                      
                                </ItemTemplate>                       
                                <HeaderStyle />
                            </asp:TemplateColumn>  
                            <asp:TemplateColumn HeaderText="质量差错">
                                <ItemTemplate>                               
                                    <asp:TextBox ID="txt7" Width=80px CssClass="text" Text='<%# DataBinder.Eval(Container.DataItem, "C_quality") %>' runat="server"/>                      
                                </ItemTemplate>                       
                                <HeaderStyle />
                            </asp:TemplateColumn>   
                            <asp:TemplateColumn HeaderText="其他">
                                <ItemTemplate>                               
                                    <asp:TextBox ID="txt8" Width=30px CssClass="text" Text='<%# DataBinder.Eval(Container.DataItem, "C_other") %>' runat="server"/>                      
                                </ItemTemplate>                       
                                <HeaderStyle />
                            </asp:TemplateColumn> 
                            <asp:TemplateColumn HeaderText="日期">
                                <ItemTemplate>                               
                                    <asp:TextBox ID="txt9" Width=80px CssClass="text" Text='<%# DataBinder.Eval(Container.DataItem, "D_overDate") %>' runat="server"/>                      
                                </ItemTemplate>                       
                                <HeaderStyle />
                            </asp:TemplateColumn>  
                            <asp:TemplateColumn HeaderText="次数">
                                <ItemTemplate>                               
                                    <asp:TextBox ID="txt10" Width=30px CssClass="text"  Text='<%# DataBinder.Eval(Container.DataItem, "D_overCount") %>' runat="server"/>                      
                                </ItemTemplate>                       
                                <HeaderStyle />
                            </asp:TemplateColumn>   
                            <asp:TemplateColumn HeaderText="小时">
                                <ItemTemplate>                               
                                    <asp:TextBox ID="txt11" Width=30px CssClass="text" Text='<%# DataBinder.Eval(Container.DataItem, "D_overHour") %>' runat="server"/>                      
                                </ItemTemplate>      
                            </asp:TemplateColumn>  
                            <asp:TemplateColumn HeaderText="(小时)">
                                <ItemTemplate>                               
                                    <asp:TextBox ID="txt12" Width=30px CssClass="text" Text='<%# DataBinder.Eval(Container.DataItem, "E_hours") %>' runat="server"/>                      
                                </ItemTemplate>      
                            </asp:TemplateColumn>    
                            <asp:TemplateColumn>
                                <ItemTemplate>                               
                                    <asp:TextBox ID="txt13" Width=80px CssClass="text" runat="server"/>                      
                                </ItemTemplate>      
                            </asp:TemplateColumn>                                                                                                                                                                                                                            
                        </Columns>
                       
     </asp:datagrid>                             
  
     </TD>
    </TR>
    
   </TABLE>
   
  </div>
    </form>
</body>
</html>
cs后台代码如下:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using userSql;

public partial class AppWebprogram_Attendance_Attrpt_Attonperrpt : System.Web.UI.Page
{
    private DataGrid m_dgDatagridToDecorate = null;
    private ArrayList m_arrHeaderCells = null;
    private Hashtable m_htblRowspanIndex = new Hashtable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

dataBind(string gIF)

        }
    }


    #region 刷新DataGrid事件
    protected void dataBind(string gIF)
    {
        Addhead();
        AttenDanceoff vpubFunc = new AttenDanceoff();
        //Response.Write(DataBase.ShowMessage(gIF));
        DataSet ds = vpubFunc.gAttkq_ResultsDs(10, 1, gIF);
        grDatalist.DataSource = ds;
        grDatalist.DataBind();
        ds.Dispose();
    }
    #endregion

    #region 按钮点击事件
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        dataBind("a");
    }
    #endregion

    #region =============创建多行表头=============================

    private DataGrid DatagridToDecorate //***事件触发************
    {
        get
        {
            return m_dgDatagridToDecorate;
        }
        set
        {
            if (null != m_dgDatagridToDecorate)
            {
                m_dgDatagridToDecorate.ItemCreated -= new DataGridItemEventHandler(DatagridToDecorate_ItemCreated);
            }
            m_dgDatagridToDecorate = value;
            m_dgDatagridToDecorate.ItemCreated += new DataGridItemEventHandler(DatagridToDecorate_ItemCreated);
        }
    }

    private void NewRenderMethod(HtmlTextWriter writer, Control ctl)
    {
        int iCurrIndex = 0;
        for (int i = 0; i < m_arrHeaderCells.Count; i++)
        {
            TableCell item = (TableCell)m_arrHeaderCells[i];
            if (item.ColumnSpan > 1)
            {
                iCurrIndex += item.ColumnSpan - 1;
            }
            if (item.RowSpan > 1)
            {
                m_htblRowspanIndex.Add(iCurrIndex + i, iCurrIndex + i);
            }
            item.RenderControl(writer);
        }
        writer.WriteEndTag("TR");
        //*** Add the style attributes that was defined in design time
        //      to our second row so they both will have the same appearance
        m_dgDatagridToDecorate.HeaderStyle.AddAttributesToRender(writer);
        //*** Insert the second row
        writer.RenderBeginTag("TR");
        //*** Render all the cells that was defined in design time, except the last one
        //      because we already rendered it above
        for (int i = 0; i < ctl.Controls.Count; i++)
        {
            if ((null == m_htblRowspanIndex[i]))
            {
                ctl.Controls[i].RenderControl(writer);
            }
        }
        //*** We don't need to write the </TR> close tag because the writer will do that for us
        //      and so we're done :)
    }

    private void DatagridToDecorate_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
        //*** Examine if the item created is the header item
        ListItemType lit = e.Item.ItemType;
        if (ListItemType.Header == lit)
        {
            //*** Redirect the default header rendering method to our own method
            e.Item.SetRenderMethodDelegate(new RenderMethod(NewRenderMethod));
        }
    }

    private void AddMergeHeader(ArrayList arrHeaderCells)
    {
        m_arrHeaderCells = arrHeaderCells;
    }

 

    private void Addhead()
    {

        TableCell cell = null;

        DatagridToDecorate = grDatalist;
        ArrayList header = new ArrayList();

        cell = new TableCell();
        cell.Text = "序号";
        cell.RowSpan = 2;
        cell.HorizontalAlign = HorizontalAlign.Center;
        header.Add(cell);

        cell = new TableCell();
        cell.Text = "姓名";
        cell.RowSpan = 2;
        cell.HorizontalAlign = HorizontalAlign.Center;
        header.Add(cell);

        cell = new TableCell();
        cell.Text = "全勤";
        cell.RowSpan = 2;
        cell.HorizontalAlign = HorizontalAlign.Center;
        header.Add(cell);


        cell = new TableCell();
        cell.Text = "休干部假";
        cell.ColumnSpan = 2;
        cell.HorizontalAlign = HorizontalAlign.Center;
        header.Add(cell);


        cell = new TableCell();
        cell.Text = "休其他假";
        cell.ColumnSpan = 2;
        cell.HorizontalAlign = HorizontalAlign.Center;
        header.Add(cell);

        cell = new TableCell();
        cell.Text = "违规(次)";
        cell.ColumnSpan = 3;
        cell.HorizontalAlign = HorizontalAlign.Center;
        header.Add(cell);

        cell = new TableCell();
        cell.Text = "节假日加班";
        cell.ColumnSpan = 3;
        cell.HorizontalAlign = HorizontalAlign.Center;
        header.Add(cell);

        cell = new TableCell();
        cell.Text = "工作日";
        cell.ColumnSpan = 1;
        cell.HorizontalAlign = HorizontalAlign.Center;
        header.Add(cell);

        cell = new TableCell();
        cell.Text = "备注";
        cell.RowSpan = 2;
        cell.HorizontalAlign = HorizontalAlign.Center;
        header.Add(cell);

        AddMergeHeader(header);
    }

 

usersql引用代码

        public DataSet gAttkq_ResultsDs(int maxRecord, int pageSige, string gIf) //*******查询,按日期查询考勤数据************
        {
            string strSql = "";
            strSql = "select id,Person_id,A_year,A_month, B_all=case when B_all=1 then '是' else '否' end,B_xjDays," +
                "B_xjhours,B_qtDays,B_qthours,C_late,C_quality,C_other,D_overDate,D_overCount,D_overHour,E_hours " +
                "from kq_Results";
            //throw new Exception(strSql);
            SqlData.pubConnecSys(ConfigurationSettings.AppSettings["SystemDb"]);
            DataSet ds = SqlData.getDataSet(dbType, "kq_Results", maxRecord, pageSige, strSql);
            SqlData.pubSysdb.Close();
            return ds;
        }

阅读全文
0 0

相关文章推荐

img
取 消
img