Skip to content

GridView with Filter Export to Excel

Preethi Ganesh

Asked Jun 28, 2016, 9:15 PM EDT1 answer
Share
LinkedInX
Hi!
 
I have a asp.net gridview with filter on each of the column the Header. If I export it to excel, the cells with the filter(like text box, dropdownlist) also appears in Excel. How can I seperate the filter from the header while exporting to excel?
 
 My Code below: 
 
protected void ExportCSV(object sender, EventArgs e)
{
 
C#
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";
        using (StringWriter sw = new StringWriter())
        {
            HtmlTextWriter hw = new HtmlTextWriter(sw);

            //To Export all pages
            gvRecipeSetup.AllowPaging = false;
            this.FilterWithPaging();

            gvRecipeSetup.HeaderRow.BackColor = Color.White;
            foreach (TableCell cell in gvRecipeSetup.HeaderRow.Cells)
            {
                cell.BackColor = gvRecipeSetup.HeaderStyle.BackColor;
            }
            foreach (GridViewRow row in gvRecipeSetup.Rows)
            {
                row.BackColor = Color.White;
                foreach (TableCell cell in row.Cells)
                {
                    if (row.RowIndex % 2 == 0)
                    {
                        cell.BackColor = gvRecipeSetup.AlternatingRowStyle.BackColor;
                    }
                    else
                    {
                        cell.BackColor = gvRecipeSetup.RowStyle.BackColor;
                    }
                    cell.CssClass = "textmode";
                }
            }

            gvRecipeSetup.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();
        }
}

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Solved it myself.
    Solution:
    To hide gridview controls:
    foreach (TableCell cell in gvRecipeSetup.HeaderRow.Cells)
    {
    Code
    cell.FindControl("txtSoftType").Visible = false;
    }
    
    For hiding edit delete columns:
    
    this.myGridview.Columns[4].Visible = false;
    0