Search code examples
asp.netvb.netgridviewexport-to-excel

Remove Edit Delete Button When Exporting GridView To Excel


I Have GridView in ASP.Net application with AutoGenerateDeleteButton & AutoGenerateEditButton Set to True. I want when exporting gridview to excel to not show these button in excel sheet. My export code below:

    Private Sub ExportGridView()
    Dim attachment As String = "attachment; filename=FileName.xls"
    Response.ClearContent()
    Response.AddHeader("content-disposition", attachment)
    Response.ContentType = "application/ms-excel"
    Dim sw As New IO.StringWriter
    Dim frm As HtmlForm = New HtmlForm()
    Page.EnableViewState = False
    Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
    frm.Attributes("runat") = "server"
    Controls.Add(frm)

    gvResults.AllowPaging = False
    gvResults.AllowSorting = False
    BindGrid()

    gvResults.DataBind()
    gvResults.Columns(14).Visible = False
    gvResults.Columns(15).Visible = False
    gvResults.Columns(16).Visible = False
    frm.Controls.Add(gvResults)
    frm.RenderControl(htw)
    Response.Write(sw.ToString())
    Response.End()
End Sub

Solution

  • After seraching i came up with this

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=User.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";
        using (StringWriter sw = new StringWriter())
        {
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            GridView1.AllowPaging = false;
            GridView1.AllowSorting = false;
            GridView1.Columns[14].Visible = false;
            GridView1.Columns[15].Visible = false;
            GridView1.Columns[16].Visible = false;
            BindGird();
            GridView1.HeaderRow.BackColor = Color.White;
            foreach (TableCell cell in GridView1.HeaderRow.Cells)
            {
                cell.BackColor = GridView1.HeaderStyle.BackColor;
            }
            foreach (GridViewRow row in GridView1.Rows)
            {
                row.BackColor = Color.White;
                foreach (TableCell cell in row.Cells)
                {
                    if (row.RowIndex % 2 == 0)
                    {
                        cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
                    }
                    else
                    {
                        cell.BackColor = GridView1.RowStyle.BackColor;
                    }
                    cell.CssClass = "textmode";
                }
            }
    
            GridView1.RenderControl(hw);
    
            //style to format numbers to string
            string style = @"<style> .textmode { } </style>";
            Response.Write(style);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
    
            GridView1.Dispose();
        }
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */
    }
    

    and on aspx page make change like

    <%@ Page Title="" Language="C#" EnableEventValidation="false"%>
    

    in vb i guess it look like this

     Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Response.Clear
        Response.Buffer = true
        Response.AddHeader("content-disposition", "attachment;filename=User.xls")
        Response.Charset = ""
        Response.ContentType = "application/vnd.ms-excel"
        Dim sw As StringWriter = New StringWriter
        Dim hw As HtmlTextWriter = New HtmlTextWriter(sw)
        GridView1.AllowPaging = false
        GridView1.AllowSorting = false
        GridView1.Columns(14).Visible = false
        GridView1.Columns(15).Visible = false
        GridView1.Columns(16).Visible = false
        BindGird
        GridView1.HeaderRow.BackColor = Color.White
        For Each cell As TableCell In GridView1.HeaderRow.Cells
            cell.BackColor = GridView1.HeaderStyle.BackColor
        Next
        For Each row As GridViewRow In GridView1.Rows
            row.BackColor = Color.White
            For Each cell As TableCell In row.Cells
                If ((row.RowIndex Mod 2)  _
                            = 0) Then
                    cell.BackColor = GridView1.AlternatingRowStyle.BackColor
                Else
                    cell.BackColor = GridView1.RowStyle.BackColor
                End If
                cell.CssClass = "textmode"
            Next
        Next
        GridView1.RenderControl(hw)
        'style to format numbers to string
        Dim style As String = "<style> .textmode { } </style>"
        Response.Write(style)
        Response.Output.Write(sw.ToString)
        Response.Flush
        Response.End
        GridView1.Dispose
    End Sub
    
    Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
    
    End Sub
    

    .aspx page is like this

    <%@ Page Title="" Language="C#" EnableEventValidation="false"%>