Hiding columns upon download from gridview to excel

H

hedgracer

I have a gridview that has six columns. I need to hide the last column
(i.e. not download it) in the download from gridview to excel. My
current code (which downloads the entire gridview) is as follows:

public static void ExportExcelFile(Control htmlData, string
filename)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition",
"attachment;filename="
+ filename + ".xls");
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.ContentType = "application/
vnd.xls";
System.IO.StringWriter stringWrite = new
System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new
HtmlTextWriter(stringWrite);
htmlData.RenderControl(htmlWrite);
HttpContext.Current.Response.Write(stringWrite.ToString());
HttpContext.Current.Response.End();
}

I am calling this by the following code:

protected void btnDownloadDebit_Click(object sender, EventArgs e)
{
grvDebit.Columns[5].Visible = false;
Util.ExportExcelFile(grvDebit, "DebitDetail");
grvDebit.Columns[5].Visible = true;

}

I thought that making the fifth column's visible property to false
before the call would result in that column not being downloaded. I
was wrong. Can anyone give me some advice on this? Thanks for all help
in advance.

Dave
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top