Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

exporting gridview to excel

Status
Not open for further replies.

mrp9090

Programmer
May 22, 2006
71
GB
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?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top