GridView - Removing Edit and Delete columns while exporting to Excel

asp.net, export-to-excel, gridview

Solution

You need to hide the individual cells in the header and data rows, like this:

// Hides the first column in the grid (zero-based index)
GridView1.HeaderRow.Cells[0].Visible = false;

// Loop through the rows and hide the cell in the first column
for (int i = 0; i < GridView1.Rows.Count;i++ )
{
    GridViewRow row = GridView1.Rows[i];
    row.Cells[0].Visible = false;
}

Problem

Thank you for viewing my question and helping. I have a asp.net application built in C#. The application has a simple gridview. I have a button to export the gridview into excel, which works as well. However, my gridview has a edit link in column 1 and a delete button in column 2. Both if these columns export to excel as well. How do I stop these two columns from exporting? I have tried a few things: ``` GridView1.Columns.RemoveAt(0); GridView1.Columns.RemoveAt(1); ``` and... ``` GridView1.Column(0).visible = false; ``` I did a data bind after each of these while trying, but it simply does not work. I'll post my code below, and maybe someone can help me out! Thanks! ``` public void ExportGridToExcel(GridView grdGridView, string fileName) { Response.Clear(); Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", fileName)); Response.Charset = ""; Response.ContentType = "application/vnd.xls"; StringWriter stringWrite = new StringWriter(); HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); grdGridView.RenderControl(htmlWrite); GridView1.Columns.RemoveAt(0); Response.Write(stringWrite.ToString()); Response.End(); GridView1.AllowPaging = false; } ``` The girdview has the edit feature set to true, and the delete button is a template field as below: ``` <Columns> <asp:TemplateField> <ItemTemplate> <asp:Button ID="btnDelete" CommandName="Delete" runat="server" Text="Delete" CssClass="okbutton" OnClientClick="return confirm('Are you sure you want to delete this entry?');" /> </ItemTemplate> </asp:TemplateField> ```

Original source

Related problems