gridView动态绑定和打印

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
    <script type="text/javascript" language="javascript">
        function printPage()
        {
            var newWin = window.open('printer','','');
            var titleHTML = document.getElementById("divshow").innerHTML;
            newWin.document.write(titleHTML);
            newWin.document.location.reload();
            newWin.print();

            newWin.close();
        }
    </script>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="生成GridView" OnClick="Button1_Click" /><br />
        <asp:GridView ID="gdshow" runat="server" Height="295px" Width="561px" >
        </asp:GridView><br />
        <%--<asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatDirection="Horizontal"
             Width="518px">
             <asp:ListItem Value="身份证号码"></asp:ListItem>
             <asp:ListItem>姓名</asp:ListItem>
             <asp:ListItem>月工资</asp:ListItem>
             <asp:ListItem>季度工资</asp:ListItem>
             <asp:ListItem>奖金</asp:ListItem>
             <asp:ListItem>年薪</asp:ListItem>
             <asp:ListItem>部门</asp:ListItem>
         </asp:CheckBoxList> --%>
     <div id="divshow" runat="server" >
        <%--在这里放置动态生成的gridview--%></div>
        <a href="javascript:;" onclick="printPage()">打印</a>
    </div>
    </form>
</body>
</html>

 

 

 

后台代码:

 

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;

 

public partial class GridViewDemo : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    /**/
    /// <summary>
    /// 绑定生成GridView
    /// </summary>
    /// <param name="gdv">要绑定的GridView</param>
    /// <param name="dtblDataSource">GridView的数据源</param>
    /// <param name="strDataKey">GridView的DataKeyNames</param>
    public void GridViewBind()
    {
        DataSet dt = getDataTable();  //获得数据源
        DataTable dts = dt.Tables[0];
        gdshow = new GridView();
        gdshow.Width = Unit.Pixel(700);
        gdshow.AutoGenerateColumns = false;
        //gvshow.RowEditing += new GridViewEditEventHandler(gvshow_RowEditing); //添加编辑事件
        gdshow.DataSource = dt;

        BoundField bf4 = new BoundField();
        bf4.HeaderText = "emp_id";
        bf4.DataField = "emp_id";
        gdshow.Columns.Add(bf4);
        //gdshow.Controls.Add(gdshow);
        gdshow.DataBind();

        //for (int i = 0; i < dts.Columns.Count; i++)
        //{
        //    BoundField bc = new BoundField();
        //    bc.DataField = dts.Columns[i].ColumnName.ToString();
        //    bc.HeaderText = dts.Columns[i].Caption.ToString();
        //    gdshow.Columns.Add(bc);
        //    gdshow.DataBind();

        //}
        //CommandField cf = new CommandField();
        ////cf.ButtonType = ButtonType.Link;
        ////cf.ShowEditButton = true;
        ////cf.CausesValidation = false;
        //gdshow.Columns.Add(cf);
        //gdshow.DataBind();

        divshow.Controls.Add(gdshow);

    }

 

    protected void Button1_Click(object sender, EventArgs e)
    {
        GridViewBind();
    }
    //protected void gvshow_RowEditing(object sender, GridViewEditEventArgs e)
    //{
    //int i = e.NewEditIndex;
    //string id = gvshow.Rows[i].Cells[0].Text.ToString();

    //Response.Write("you want to edit" + id);
    //}


    public DataSet getDataTable()
    {
        string sql = "select emp_id,bankNo,workMode,iPay,iMeal,iEarning,iTaxbase,iDeduct,iAmount,ideal from tbl_employe_pay";
        DataSet dt = Dal.DbHelperSQL.Query(sql);
        return dt;
    }
}

 

简单的例子,还可以优化,等我有时间再做这个

posted @ 2008-09-11 11:02  艾伦  阅读(1513)  评论(0编辑  收藏  举报