我有一个gridview绑定了数据,现在有个问题 我其中有一列数据 是我绑定了gridview后 再通过方法赋值上去的(需求所以必须得这么做),这样页面是能显示数据的,但是当我导出这个girdview的时候  却没有把这列值导出去,导出的别的绑定的列都有值 就是这列后面赋值上去的没有 
请问有什么方法可以 把这列的数据也一并导出吗?this.stopGV.DataSource = DataDataTable;
                    this.stopGV.DataBind();
                    string x = "";
                    for (int i = 0; i < this.stopGV.Rows.Count; i++)
                    {
                        Label lbaddress = (Label)this.stopGV.Rows[i].Cells[7].FindControl((Convert.ToInt32(this.stopGV.Rows[0].Cells[0].Text) + i).ToString());
                        x += "showAddress(" + (Convert.ToInt32(this.stopGV.Rows[0].Cells[0].Text)+i) + "," + this.stopGV.Rows[i].Cells[3].Text + "," + this.stopGV.Rows[i].Cells[4].Text + ");";
                        //lbaddress.Attributes.Add("onload", "showAddress(onetwo,121.557284,29.825411);");
                    }
                    ScriptManager.RegisterClientScriptBlock(this, Page.GetType(), "x", x, true);
-----------------------------这个是我绑定好后 再添加列值的方法  能在页面显示 Export("application/ms-excel", "停车统计" + DateTime.Now.ToString("yyMMddhhmmss") + ".xls", stopGV);
——————————————这个是我导出数据的代码  stopGV就是gridview小弟知道这样只能导出绑定的数据 有没有大虾能帮忙 用别的方法 让我把这个列值也导出去分数不多 ,感激不尽

解决方案 »

  1.   

    吧你上面得到的数据源,转成DataTable然后加一列不就得了
      

  2.   

    可以实现直接导出gridview显示的数据,而不是根据数据源
      

  3.   

    没有细看你的问题,我是这么导出的,看能不能用上
    Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("Content-Disposition", "attachment;   filename=" + System.Web.HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8) + ".xls");//这样的话,可以设置文件名为中文,且文件名不会乱码。其实就是将汉字转换成UTF8
            Response.ContentEncoding = System.Text.Encoding.UTF7;
            Response.Charset = "gb2312";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
            Response.ContentType = "application/vnd.xls";
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);        if (GridViewPlanFee.AllowPaging == true)
            {
                GridViewPlanFee.AllowPaging = false;  //如果存在分页就要设置这个 先不让他分页 然后在绑定 当然也可以做到导出当前页或者导出全部!看自己需要啦
                GridViewPlanFee.DataBind();//重新绑定一次
                GridViewPlanFee.RenderControl(htmlWrite);
                Response.Output.Write(stringWrite.ToString());
                Response.Flush();
                Response.End();
                GridViewPlanFee.AllowPaging = true;
                GridViewPlanFee.DataBind();
            }
            else
            {
                GridViewPlanFee.RenderControl(htmlWrite);
                Response.Output.Write(stringWrite.ToString());
                Response.Flush();
                Response.End();
            }
      

  4.   

    这个应该是通过数据源导出的吧 不是直接导出gridview显示的数据
      

  5.   

    代码如下 protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindData();
                }
            }
            private void BindData()
            {
                // make the query 
                string query = "SELECT * FROM Stable";
                SqlConnection myConnection = new SqlConnection(ConnectionString);
                SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);
                DataSet ds = new DataSet();
                ad.Fill(ds, "Stable");
                GridView1.DataSource = ds;    
                GridView1.DataKeyNames = new string[] { "SID" };//主键
                GridView1.DataBind();
            }
            //导出Excel,带提示保存。
            protected void Button1_Click(object sender, EventArgs e)
            {
                //GridView1.Columns.Insert(5, column); //添加第6 列
                if (GridView1 != null)
                {
                    Response.Clear();
                    Response.Buffer = true;
                    Response.Charset = "GB2312";
                    Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("订卷统计") + ".xls");
                    // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
                    Response.ContentEncoding = System.Text.Encoding.UTF7;
                    Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
                    System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
                    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
                    this.GridView1.RenderControl(oHtmlTextWriter);
                    Response.Output.Write(oStringWriter.ToString());
                    Response.Flush();
                    Response.End();
                }
                else
                {
                    this.Response.Write("<script language=javascript>alert('没有符合条件的记录!')</script>");
                }
            }
            public override void VerifyRenderingInServerForm(Control control)
            {
                // Confirms that an HtmlForm control is rendered for
            }
            protected override void OnInit(EventArgs e)
            {
                TemplateField customField = new TemplateField();
                customField.ShowHeader = true;
                customField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "添加行");//新添加行的Header
                GridViewTemplate gvt = new GridViewTemplate(DataControlRowType.EmptyDataRow, "lbtn", "");//添加一个空行
                gvt.eh += new GridViewTemplate.EventHandler(lbtn_Click);
                customField.ItemTemplate = gvt;
                GridView1.Columns.Add(customField);
                base.OnInit(e);
            }
            public class GridViewTemplate : ITemplate
            {
                public delegate void EventHandler(object sender, EventArgs e);
                public event EventHandler eh;
                private DataControlRowType templateType;
                private string columnName;
                private string controlID;
                public GridViewTemplate(DataControlRowType type, string colname)
                {
                    templateType = type;
                    columnName = colname;
                }
                public GridViewTemplate(DataControlRowType type, string controlID, string colname)
                {
                    templateType = type;
                    this.controlID = controlID;
                    columnName = colname;
                }
                public void InstantiateIn(System.Web.UI.Control container)
                {
                    switch (templateType)
                    {
                        case DataControlRowType.Header:
                            Literal lc = new Literal();
                            lc.Text = columnName;
                            container.Controls.Add(lc);
                            break;
                        case DataControlRowType.DataRow:
                            LinkButton lbtn = new LinkButton();
                            lbtn.ID = this.controlID;
                            if (eh != null)
                            {
                                lbtn.Click += new System.EventHandler(eh);
                            }
                            lbtn.DataBinding += new System.EventHandler(lbtn_DataBinding);                        container.Controls.Add(lbtn);                        break;
                        default:
                            break;
                    }
                }
                void lbtn_DataBinding(object sender, EventArgs e)
                {
                    LinkButton lbtn = sender as LinkButton;
                    if (lbtn != null)
                    {
                        GridViewRow container = lbtn.NamingContainer as GridViewRow;
                        if (container != null)
                        {
                            object dataValue = DataBinder.Eval(container.DataItem, columnName);
                            if (dataValue != DBNull.Value)
                            {
                                lbtn.Text = dataValue.ToString();
                            }
                        }
                    }
                }        }
            protected void lbtn_Click(object sender, EventArgs e)
            {
                ClientScript.RegisterStartupScript(GetType(), "test", "alert('ok');", true);
            }至于上面的GridView动态添加模板列 .参考sandy945写的