Reputation: 41
I'm here using GridView
to show data. And for exporting Gridview
data into excel format, I'm using a button - "Download into Excel"
. The downloading into Excel format or Exporting into Excel is working perfectly. That GridView
is showing total of 9 Columns.
My problem is I wanna show 7 columns (as per our choice) among them in the Excel Sheet.
The GridView
which is showing should be remain as usual but Excel will show only those columns which we want.
I've already used GridView1.Columns.RemoveAt(2);
But this removes randomly more numbers of columns.
Upvotes: 0
Views: 8004
Reputation: 1
try adding this code line before you export the file
gridview.Columns[x].Visible = false;
Upvotes: 0
Reputation: 11
RemoveAt() won't work
try something like this
int i;
for (i = 0; i < GV.Columns.Count; i++)
{
if (GV.Columns[i].HeaderText == "Title")
{
GV.Columns[i].Visible = false;
break;
}
}
GV.RenderControl(htmltextwrtter);
Response.Write(strwritter.ToString());
Response.Flush();
Response.End();
GV.Columns[i].Visible = true;
Upvotes: 1
Reputation: 1
GridView1.Columns(0).Visible = False
replace 0 with the column you want to remove (it start counting from 0,1,2~ so on) it work on my gridview excel reporting.
Upvotes: 0
Reputation: 908
You need to databind the gridview after using the RemoveAt function:
GridView1.Columns.RemoveAt(0);
GridView1.DataBind();
Upvotes: 2
Reputation: 51
.Net 4.0; VS2012
In my similar case - - roughly what Kedar has described, including the button and with seeing 100% of the grid exported - - I've concluded it's futile to try to get the RemoveAt(index) to do its job to remove a column from a gridview I'm exporting. I don't know why it doesn't, but my gridview is always unchanged after the RemoveAt.
I've seen a similar question on a board where the accepted answer - -to a poster who had wanted to export to Excel, again wanting to hide specified columns upon export - -was to just make his unwanted columns not .Visible. The respondent, though kind to contribute, had missed the point: The goal was to have the columns in question not appear on the EXPORT - - not just disappear from the screen display, where hiding columns is straightforward (set Visible=false).
In my case, I'm trying to lose columns on the gridview, when it's sent to Excel, WITHOUT rebinding and without introducing sql that's dynamic. I think it may be a lost cause, since I just don't see RemoveAt(index) working, even though no errors are caused. I'm mystified what I could be doing that causes gridview.Columns.RemoveAt(index) to very reliably not perform its function: All columns persist in being present after I've done it.
In the following code, my goal has been reduced to trying to remove a single column, to verify RemoveAt(index) will do anything at all.
GridView gProgramProposalRequests appears in the UI.
I'm making a copy of it, gridForExport, because I want to remove columns, then pass gridForExport to an export method.
"Title" is one of the columns in my grid, used as the example column in this run.
GridView gridForExport = (GridView)gProgramProposalRequests;
gridForExport.AutoGenerateColumns = false;
for (int i = 0; i < gridForExport.Columns.Count; i++)
{
if (gridForExport.Columns[i].HeaderText == "Title")
gridForExport.Columns.RemoveAt(i);
}
After the loop, "Title" is still present. I thought I had told the app to Remove that column named Title. I watch the execution cursor perform the RemoveAt line. It doesn't matter which column I specify - -after this loop executes, I can still find whichever column I had named.
Followup - -I had no success trying to drop columns from the GridView's collection. Now I'm sorta liking the results I'm getting from writing CSV content to the response instead, blocking my specified columns from being added. But I'll have to figure out how to handle commas in the content fields. Thanks to http://wiki.asp.net/page.aspx/401/export-to-csv-file/
Upvotes: 3