刚完成一个很小项目的技巧总结

最近做了个很小的项目其中要用到GRIDVIEW自定义的列显示,从网上找了一些资料最后可以实现了,自定义的列显示



如图所示。
具体实现在gridview的RowCreated事件中处理

 if (e.Row.RowType == DataControlRowType.Header )
        {
            Table tb = (Table)((GridView)(sender)).Controls[0];
            GridViewRow rowHeader = new GridViewRow(0  ,0 , DataControlRowType.Header  , DataControlRowState.Normal);
            rowHeader.BackColor = System.Drawing.Color.FromName ("#e3efff");
             rowHeader.Font.Bold = true;

             TableCellCollection cells = e.Row.Cells;
             TableCell headerCell = new TableCell();

             headerCell = new TableCell();
             #region 数据统计
             string qy = "select count(是否签约),sum(合同金额) from 客户信息表 where 是否签约=1 and 签约时间>='" +txtqyrq .Text .Trim ()+ "' and 签约时间<='" + txtqyrq2 .Text .Trim () + "'";
             string lbqy = "";
             string lhhtje = "";
             string lhsb = "";
             DataSet ds = DbHelperSQL.Query(qy);
             if (ds.Tables[0].Rows.Count > 0)
             {
                 lbqy = ds.Tables[0].Rows[0][0].ToString();
                 lhhtje = ds.Tables[0].Rows[0][1].ToString();
             }
             lhsb = Convert.ToString(Convert.ToInt32(Show ()) - Convert.ToInt32(lbqy.Trim()));
             #endregion

             headerCell.Text = "共有:";
             headerCell.Text += Show ().ToString ();
             headerCell.Text += "条记录,签约:";
             headerCell.Text += lbqy;
             headerCell.Text += " 条记录,签约金额为:";
             headerCell.Text +=lhhtje ;
             headerCell.Text += "$,未签约为:";
             headerCell.Text += lhsb;
             headerCell.Text += "条记录。";
             headerCell.ColumnSpan = 10;
             headerCell.HorizontalAlign = HorizontalAlign.Left ;
             rowHeader.Cells.Add(headerCell);
             rowHeader.Visible = true;
             //tb.Controls .AddAt(0 , rowHeader);
             tb.Rows.AddAt(0, rowHeader);
             e.Row.Visible = true;
        }

为了实现把自定的字段导出保存。实现方法如下。注意:一定要在导出调用rowCreate 事件。


  string fileName = "客户信息";
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "gb2312";
        //下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开
        //ilename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc, .xls,.txt,.htm  
        Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ".xls");
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
        //Response.ContentType指定文件类型 可以为application/ms-excel    application/ms-word    application/ms-txt    application/ms-html    或其他浏览器可直接支持文档 
        Response.ContentType = "application/ms-excel";
        //设置DataGrid的DataSource为DataTable,这样可以使excel输出没有页脚等,只有数据部分
        //去出生年月时要在数据前加上&nbsp,例如('&nbsp'+chushengnianyue) as 出生年月
        System.Web.UI.WebControls.GridView myDataGrid = new System.Web.UI.WebControls.GridView();

        myDataGrid.RowCreated += new GridViewRowEventHandler(dg_RowCreated);//此处非常重要
   //     myDataGrid.RowDataBound += new GridViewRowEventHandler(dg_RowDataBound);
        myDataGrid.DataSource = this.dg.DataSource;
        myDataGrid.DataBind();
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        myDataGrid.RenderControl(oHtmlTextWriter);
     
        //this 表示输出本页,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件  
        Response.Write(oStringWriter.ToString());
       
        Response.End();


posted @ 2008-04-14 15:16  NewSoftsNet  Views(271)  Comments(0Edit  收藏  举报