exporting gridview to excel

M

Mike P

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

protected void btnExport_Click(object sender, ImageClickEventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition",
"attachment;filename=FileName.xls");

Response.Charset = "";

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

// comment out the line below

// Response.Cache.SetCacheability(HttpCacheability.NoCache);

Response.ContentType = "application/vnd.ms-excel";

System.IO.StringWriter stringWrite = new
System.IO.StringWriter();

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

GridView1.RenderControl(htmlWrite);

Response.Write(stringWrite.ToString());

Response.End();
}

public override void VerifyRenderingInServerForm(Control 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.Text)
{
case "0.25":
e.Row.Cells[4].Text = "A (25%)";
e.Row.Cells[4].BackColor =
System.Drawing.Color.PowderBlue;
break;

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

Can anybody help with this?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top