exporting gridview to excel

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike P

    exporting gridview to excel

    I am using the following code to export my gridview to excel :

    protected void btnExport_Click (object sender, ImageClickEvent Args e)
    {
    Response.Clear( );
    Response.AddHea der("content-disposition",
    "attachment;fil ename=FileName. xls");

    Response.Charse t = "";

    // If you want the option to open the Excel file without saving
    than

    // comment out the line below

    // Response.Cache. SetCacheability (HttpCacheabili ty.NoCache);

    Response.Conten tType = "applicatio n/vnd.ms-excel";

    System.IO.Strin gWriter stringWrite = new
    System.IO.Strin gWriter();

    System.Web.UI.H tmlTextWriter htmlWrite = new
    HtmlTextWriter( stringWrite);

    GridView1.Rende rControl(htmlWr ite);

    Response.Write( stringWrite.ToS tring());

    Response.End();
    }

    public override void VerifyRendering InServerForm(Co ntrol control)
    {
    // Confirms that an HtmlForm control is rendered for the
    specified ASP.NET server control at run time.
    }

    My problem is that I am setting the text values of one of the columns in
    the RowDataBound event, and doing so seems to mean that the text for
    this column is not exported, whereas if I don't set the text in the
    RowDataBound event, the text is exported.

    Here is my code for RowDataBound:

    switch (Probability.Te xt)
    {
    case "0.25":
    e.Row.Cells[4].Text = "A (25%)";
    e.Row.Cells[4].BackColor =
    System.Drawing. Color.PowderBlu e;
    break;

    case "0.5":
    e.Row.Cells[4].Text = "B (50%)";
    e.Row.Cells[4].BackColor =
    System.Drawing. Color.DeepSkyBl ue;
    break;

    Can anybody help with this?



    *** Sent via Developersdex http://www.developersdex.com ***
Working...